関数型言語ML (SML, OCaml, etc.), Part 8

2024/11/01(金) 11:18:17.62ID:MT2bV/S9
関数型言語MLについて語るスレッドです。

MLは、確固とした理論的背景を持つ言語でありながら、
現実的なソフトの開発にも使用できる実用性を備えた言語です。
また、プログラミングの初心者が最初に学習する言語としても優れています。

総本山
Standard ML https://www.smlnj.org/
Objective Caml https://ocaml.org/

前スレ
関数型言語ML (SML, OCaml, etc.), Part 7
https://mevius.5ch.net/test/read.cgi/tech/1509524735/
2024/11/01(金) 18:37:35.02ID:nrkm3PTC
関数型言語ML (SML, OCaml, etc.), Part 6
http://mevius.2ch.net/test/read.cgi/tech/1245017721/

関数型言語ML(SML, OCaml, etc.), Part 5
http://pc12.2ch.net/test/read.cgi/tech/1186292994/

関数型言語ML(SML, OCaml, etc.), Part 4
http://pc11.2ch.net/test/read.cgi/tech/1133003340/

関数型言語ML(SML, OCaml, etc.), Part 3
http://pc8.2ch.net/test/read.cgi/tech/1103606223/

let t = 関数型プログラミング言語ML 2
http://pc5.2ch.net/test/read.cgi/tech/1058630709/

関数型プログラミング言語ML
http://pc2.2ch.net/test/read.cgi/tech/1012445015/
2024/11/02(土) 16:39:45.15ID:Z24kzP6m
ML (programming language) 1973年に登場
https://en.wikipedia.org/wiki/ML_(programming_language)

J. Roger Hindley 1939年生まれ
https://en.wikipedia.org/wiki/J._Roger_Hindley

Robin Milner 1934年生まれ 2010年没
https://en.wikipedia.org/wiki/Robin_Milner
2024/11/02(土) 16:54:43.41ID:Z24kzP6m
◎SMLで関数の定義いろいろ
fun factorial n =
if n = 0 then 1 else n * factorial (n - 1)

◎clausal function definitions
fun factorial 0 = 1
| factorial n = n * factorial (n - 1)

◎whileもある
fun factorial n = let val i = ref n and acc = ref 1 in
while !i > 0 do (acc := !acc * !i; i := !i - 1); !acc
end

◎ラムダ関数として
val rec factorial = fn 0 => 1 | n => n * factorial (n - 1)

Standard ML
https://en.wikipedia.org/wiki/Standard_ML
2024/11/02(土) 20:34:17.15ID:0jOsGELW
◎OCamlで関数の定義いろいろ
let rec factorial n =
if n = 0 then 1 else n * factorial (n - 1)

◎whileもある
let factorial n = let i = ref n and acc = ref 1 in
while !i > 0 do acc := !acc * !i; i := !i - 1; done; !acc

◎forもある
let factorial n = let acc = ref 1 in
for i = 1 to n do acc := !acc * i done; !acc

◎ラムダ関数として
let rec factorial = function 0 -> 1 | n -> n * factorial (n - 1)

◎末尾再帰
let factorial =
let rec aux acc n = if n < 1 then acc else aux (acc * n) (n - 1) in aux 1
6デフォルトの名無しさん
垢版 |
2024/11/17(日) 22:38:39.01ID:vlQbYPk+
◎Haskellで関数の定義いろいろ
factorial n =
 if n = 0 then 1 else n * factorial (n - 1)

◎ガード
factorial n
| n == 0 = 1
| otherwise = n * factorial (n - 1)

◎foldl
factorial n = foldl (*) 1 [1..n]

◎product = foldl (*) 1
factorial n = product [1..n]

◎ラムダ関数として
factorial = \n -> if n = 0 then 1 else n * factorial (n - 1)

◎末尾再帰(空白が全部1個になるので_に置き換えてます)
factorial = f 1
___where
______f a 0 = a
______f a x = f (a * x) (x - 1)
7デフォルトの名無しさん
垢版 |
2024/11/17(日) 23:01:25.43ID:vlQbYPk+
肝心のパターンマッチ忘れてた

factorial 0 = 1
factorial n = n * factorial (n - 1)
8デフォルトの名無しさん
垢版 |
2024/11/24(日) 21:33:47.09ID:7LCq6ZGA
Haskellにも一応forM/forM_ というのはあるけど、mapM/mapM_ の引数を逆にしただけ。
そして、OCaml の for版 factorial は副作用前提なので、Haskellで書くとしたらIORef使ってメモリと入出力処理をする。
(Haskellでは、メモリもファイルやIOと同じ扱い)

◎for(M)もある

import Data.IORef
import Control.Monad -- forM_ のため。mapM_ の引数を入れ替えただけなので本当は要らない子

factorial n = do x <- newIORef 1
        forM_ [1..n] (modifyIORef x.(*)) -- xに x *= 1, x *= 2 と、リストの末尾まで更新を繰り返す
        readIORef x -- x から値を取り出す
9デフォルトの名無しさん
垢版 |
2024/11/24(日) 21:36:31.64ID:7LCq6ZGA
全角の空白だと消えない。
コピペするときは注意だけど。
2025/02/08(土) 15:58:06.70ID:+2jmv8fY
どうせ今はJavaだろうがPHPだろうが関数型の機能あるんでしょ?結局はJavaでいいじゃん
レスを投稿する

5ちゃんねるの広告が気に入らない場合は、こちらをクリックしてください。

ニューススポーツなんでも実況