(* はじめて書いたOCamlプログラム、ありがちなマンデルブロート描画。*)
#load "stdlib.cma";; #load "graphics.cma";; open Complex;; open Graphics;;

let rec mandel z c n = if n > 63 then 64 else if (norm z) > 2.0 then n else
mandel (add (mul z z) c) c (n + 1);;

let col n b = if (n land (1 lsl b)) = 0 then 0 else 15 lsl (b * 4)
let color n = (col n 0)+(col n 1)+(col n 2)+(col n 3)+(col n 4)+(col n 5);;

let dx, dy={re=(0.5/.200.);im=0.}, {re=0.;im=(0.5/.200.)}
let rec draw_x x y z = set_color (color (mandel {re=0.;im=0.} z 0)); plot x y;
if x<200 then draw_x (x+1) y (add z dx)
let rec draw y z = draw_x 0 y z ; if y<200 then draw (y+1) (add z dy);;

open_graph " 201x201";;
draw 0 {re=(-1.6);im=(-0.1)};;
wait_next_event [Button_down];;
close_graph();;