Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (*kitreynolds3.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 = Lf | Br of 'a * 'a tree * 'a tree | |
26 | ||
27 | fun max(i:int, j) = if i>j then i else j | |
28 | fun member(x,[]) = false | |
29 | | member(x,x'::rest) = x=x' orelse member(x, rest) | |
30 | ||
31 | fun search p Lf = false | |
32 | | search p (Br(x,t1,t2)) = | |
33 | if member(x,p) then true | |
34 | else search (x::p) t1 orelse | |
35 | search (x::p) t2 | |
36 | ||
37 | fun mk_tree 0 = Lf | |
38 | | mk_tree n = let val t = mk_tree(n-1) | |
39 | in Br(n,t,t) | |
40 | end | |
41 | val it = if search [] (mk_tree 20) | |
42 | then print "true\n" | |
43 | else print "false\n"; |