Ken Wakita (https://wakita.github.io/fp2017/)
Oct. 5, 2017
min-caml
implementationCore (Pervasives) (Plenty)
List (16) — list operations
Str (5) — regular expression
Arg (3) — parsing of command line arguments
min-caml
implementationList.map
)# List.map string_of_int [1; 2; 3]
- : string list = ["1"; "2"; "3"]
# List.map (function n -> Array.make n 3) [3; 4; 5];;
- : int array list = [[|3; 3; 3|]; [|3; 3; 3; 3|]; [|3; 3; 3; 3; 3|]]
List.fold_left
)# List.fold_left (fun a x -> a + x) 0 [2; 3; 5; 7; 11] (* sum *);;
- : int = 28
# List.fold_left (fun a x -> a * x) 1 [2; 3; 5; 7; 11] (* product *);;
- : int = 2310
# List.fold_left (fun l x -> List.map (function l -> x :: l) ([] :: l))
[] [2; 3; 5; 7; 11];;
- : int list list = [[11]; [11; 7]; [11; 7; 5]; [11; 7; 5; 3]; [11; 7; 5; 3; 2]]
(* suffices of a list *)
ocamlopt
)(* welcome.ml *)
let print whatever =
Printf.printf "Welcome to %s!!!\n" whatever
(* main.ml *)
Welcome.print "OCaml world"
ocamlopt -o main welcome.ml main.ml
Benchmark program
(* Takeuchi's Tarai function *)
let rec t x y z =
if x <= y then y
else t (t(x - 1) y z) (t (y - 1) z x) (t (z - 1) x y)
let x, y, z = (14, 7, 0);;
Printf.printf "Takeuchi(%d, %d, %d) = ...%t" x y z flush;
$ ocamlc -o tak tak.ml
$ time ./tak
Takeuchi(15, 8, 0) = ... 15
./tak 72.69s user 0.27s system 99% cpu 1:13.56 total
$ ocamlopt -o tak tak.ml
$ time ./tak
Takeuchi(15, 8, 0) = ... 15
./tak 6.46s user 0.04s system 98% cpu 6.564 total
x12 acceleration in ocamlopt!
ocamllex
)ocamlyacc
)