#ocamlbook#
標準ライブラリからの抜粋。これは説明したほうがいいんじゃね?というやつ。
http://ocaml.jp/archive/ocaml-manual-3.06-ja/libref/Pervasives.html
# abs;; - : int -> int = <fun> #
引数の絶対値を返します。
# abs (-1);; - : int = 1 #
# abs 1;; - : int = 1 #
# (mod);; - : int -> int -> int = <fun> #
余りを返します。
# (mod) 3 2;; - : int = 1 #
# 3 mod 2;; - : int = 1 #
2つ目の引数が0の場合には、例外を発生させます。
# 3 mod 0;; Exception: Division_by_zero. #
http://ocaml.jp/archive/ocaml-manual-3.06-ja/libref/List.html
リストの長さを返します。
# List.length [1;2;3];; - : int = 3 #
hdは、リストの先頭要素を返します。
# List.hd [1;2;3];; - : int = 1 #
空リストに適用した場合には、例外を発生させます。
# List.hd [];; Exception: Failure "hd". #
tlはhdの反対で、リストの先頭要素を除いた残りを返します。
# List.tl [1;2;3];; - : int list = [2; 3] #
こちらも、空リストに適用すると例外を発生させます。
# List.tl [];; Exception: Failure "tl". #
与えられたリストの要素の順番を反転したものを返します。
# List.rev [1;2;3];; - : int list = [3; 2; 1] #
与えられた"リストのリスト"を結合し、1つのリストにします。
# List.concat [[1;2];[3;4];[5;6]];; - : int list = [1; 2; 3; 4; 5; 6] #
引数に関数とリストを取って、リストの各要素に関数を適用する関数です。関数型言語らしい関数ですね。
# List.map Char.uppercase ['a';'b';'c'];; - : char list = ['A'; 'B'; 'C'] #
Charモジュールのuppercaseは、小文字を大文字に変える関数です。mapは、
mapとは写像のことで、この場合は、小文字の集合を大文字の集合に写像していることになりますね。
fold_leftは、畳み込み関数というものです。この関数は、畳み込みに使われる関数fと初期値とリストを取って、現在値とリストの要素を順にfを使って畳み込んでいきます。例えば、リストの要素の和を計算する関数sumは、次のように定義できます。
# let sum numbers = List.fold_left (+) 0 numbers;; val sum : int list -> int = <fun> # sum [1;2;3;4;5];; - : int = 15 #
これは、次のように展開して考えるとわかりやすいかもしれません。
sum [1;2;3;4;5] = (((((0+1)+2)+3)+4)+5)
http://ocaml.jp/archive/ocaml-manual-3.06-ja/libref/Array.html
http://ocaml.jp/archive/ocaml-manual-3.06-ja/libref/Printf.html
http://ocaml.jp/archive/ocaml-manual-3.06-ja/libref/Hashtbl.html
http://ocaml.jp/archive/ocaml-manual-3.06-ja/libref/Stream.html