なんかやはり enum は print できないとダメですよね!ということで MetaOCaml は中一までだったかもと思ったとか。http://d.hatena.ne.jp/sumii/20060918/1158537612#c1158545128 type a = A type b = B of int let _ = Gprint.eprint "aaaa"; Gprint.eprin…
OCaml は小学生までかなぁと思ったので立派な中二になるべく。とりあえず unless でも書いてみる。 let unless cond no = if not cond then no else .< () >. let _ = .! (unless true .<print_string "hello true\n">.); .! (unless false .<print_string "hello false\n">.);読みにくいなぁ。ちょっとした構文拡張に使</print_string></print_string>…
なんか関数の最後で再帰呼び出しすると勝手にループになるらしいですよ。まぁ適当にデクリメントしながら画面に表示する関数を書く。 let rec print_decrement x = if x = 0 then () else ( print_int x; print_decrement (x-1); ) let _ = print_decrement …
パターンマッチとリストは重要なのだった。 C++ の for_each みたいなのを書くとして、 let rec for_each l f = match l with | [] -> () | h :: t -> f h; for_each t f let _ = for_each ["hoge"; "hage"; "hige"] print_endlineなんかこんなん。パターン…
union といえば(ていうか union じゃなくて variant 型とか言うらしいですが)、なんか option 型とかいうのが組み込まれてて、なにやらこれが便利で、えーと無効な値を表現できるとかそんな物体で。 let div x y = if y = 0 then None else Some (x / y) let…
なんかやっと union の話なのか。なんか enum の方と似たような定義するわりには割と別の物体というか。要は C でよくやる struct VALUE { int type; union { int i; float f; char* s; } val; }みたいなのを両方一気に定義できちゃう、みたいなそんなノリ。…
ゲーム脳と OO 脳は不治の病なのでとりあえず OO は使わん方向で考えることになっています。型は全部 type 予約語でやるんだけど、やり方によって役割変わりまくりというか。 (* typedef *) type pai = int (* enum *) type tsuhai = E | S | W | N | H | T …