| 1 |
(*kitreynolds2.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 |
fun rev l = (* linear-time reversal of lists! *) |
| 11 |
let fun loop([], acc) = acc |
| 12 |
| loop(x::xs, acc) = loop(xs, x::acc) |
| 13 |
in |
| 14 |
loop(l, []) |
| 15 |
end |
| 16 |
|
| 17 |
fun foldR f b [] = b |
| 18 |
| foldR f b (x::xs) = f x (foldR f b xs) |
| 19 |
|
| 20 |
fun curry f x y = f(x,y) |
| 21 |
|
| 22 |
datatype 'a Option = None | Some of 'a |
| 23 |
|
| 24 |
|
| 25 |
datatype 'a tree = |
| 26 |
Lf |
| 27 |
| Br of 'a * 'a tree * 'a tree |
| 28 |
|
| 29 |
fun max(i:int, j) = if i>j then i else j |
| 30 |
fun search p Lf = false |
| 31 |
| search p (Br(x,t1,t2)) = |
| 32 |
if p x then true |
| 33 |
else search (fn y => y=x orelse p y) t1 orelse |
| 34 |
search (fn y => y=x orelse p y) t2 |
| 35 |
|
| 36 |
fun mk_tree 0 = Lf |
| 37 |
| mk_tree n = let val t = mk_tree(n-1) |
| 38 |
in Br(n,t,t) |
| 39 |
end |
| 40 |
val it = if search (fn _ => false) (mk_tree 20) then print "true\n" |
| 41 |
else print "false\n" |
| 42 |
|