ocamlのgraphicsモジュール

ocamlが一番絵を描くのが簡単な言語かもしれません。
標準で、絵を描くためのモジュールがついてきます。

(* draw.ml *)
(* graphics.cma と unix.cmaを使う *)
open Graphics
let color_size = 255

let rec plot_graph x (r,g,b) n f=
  let rec loop (r,g,b) n =
      if n > color_size then
	()
      else
	begin
	  set_color (rgb r g b);
	  draw_segments [|(x,n, x+10,n)|];
	  loop (f (r,g,b)) (n+1)
	end in
    loop (r,g,b) n

let f x f = plot_graph x (0,0,0) 0 f

let () = 
  open_graph " 600x600";
  f 0 (fun (r,g,b) -> (r+1,g+1,b+1));
  f 10 (fun (r,g,b) -> (r+1,g,b));
  f 20 (fun (r,g,b) -> (r+1,g+1,b));
  f 30 (fun (r,g,b) -> (r+1,g+1,b+1));
  f 40 (fun (r,g,b) -> (r+1,g,b+1));
  f 50 (fun (r,g,b) -> (r,g+1,b));
  f 60 (fun (r,g,b) -> (r,g+1,b+1));
  f 70 (fun (r,g,b) -> (r,g,b+1));
  Unix.sleep 5;
  close_graph ()

実行するには

unix.cmaとgraphics.cmaは、draw.mlよりも先に置かないと、errorを出してしまいます。

ocamlc -o draw unix.cma graphics.cma draw.ml && ./draw