| 1 |
(*kitmandelbrot.sml*) |
| 2 |
|
| 3 |
fun digit n = chr(ord #"0" + n) |
| 4 |
fun digits(n,acc) = |
| 5 |
if n >=0 andalso n<=9 then digit n:: acc |
| 6 |
else digits (n div 10, digit(n mod 10) :: acc) |
| 7 |
|
| 8 |
fun int_to_string(n) = implode(digits(n,[])) |
| 9 |
|
| 10 |
(* mandelbrot.sml *) |
| 11 |
|
| 12 |
(* |
| 13 |
structure Main : BMARK = |
| 14 |
struct |
| 15 |
*) |
| 16 |
val x_base = ~2.0 |
| 17 |
val y_base = 1.25 |
| 18 |
val side = 2.5 |
| 19 |
|
| 20 |
val sz = 2048 |
| 21 |
val maxCount = 1024 |
| 22 |
|
| 23 |
val delta = side / (real sz) |
| 24 |
|
| 25 |
val sum_iterations = ref 0 |
| 26 |
|
| 27 |
fun loop1 i = if (i >= sz) |
| 28 |
then () |
| 29 |
else let |
| 30 |
val c_im : real = y_base - (delta * real i) |
| 31 |
fun loop2 j = if (j >= sz) |
| 32 |
then () |
| 33 |
else let |
| 34 |
val c_re = x_base * (delta + real j) |
| 35 |
fun loop3 (count, z_re : real, z_im : real) = if (count < maxCount) |
| 36 |
then let |
| 37 |
val z_re_sq = z_re * z_re |
| 38 |
val z_im_sq = z_im * z_im |
| 39 |
in |
| 40 |
if ((z_re_sq + z_im_sq) > 4.0) |
| 41 |
then count |
| 42 |
else let |
| 43 |
val z_re_im = (z_re * z_im) |
| 44 |
in |
| 45 |
loop3 (count+1, |
| 46 |
(z_re_sq - z_im_sq) + c_re, |
| 47 |
z_re_im + z_re_im + c_im) |
| 48 |
end |
| 49 |
end (* loop3 *) |
| 50 |
else count |
| 51 |
val count = loop3 (0, c_re, c_im) |
| 52 |
in |
| 53 |
sum_iterations := !sum_iterations + 1(*count*); |
| 54 |
loop2 (j+1) |
| 55 |
end |
| 56 |
in |
| 57 |
loop2 0; |
| 58 |
loop1 (i+1) |
| 59 |
end |
| 60 |
|
| 61 |
fun doit () = (sum_iterations := 0; loop1 0) |
| 62 |
|
| 63 |
fun testit () = ( |
| 64 |
sum_iterations := 0; |
| 65 |
loop1 0; |
| 66 |
print(int_to_string(!sum_iterations) ^ " iterations\n")) |
| 67 |
(* |
| 68 |
end (* Mandelbrot *) |
| 69 |
*) |
| 70 |
|
| 71 |
(*val _ = (doit (); doit(); doit());*) |
| 72 |
|
| 73 |
val _ = (testit (); testit (); testit ()); (* this should give ``1084512 iterations'' *) |