ETO RELAX BRATISHKI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
datatype IntTree
= NODE of int * (unit -> IntTree) * (unit -> IntTree)
| LEAF
fun getLevel(NODE (v, n1, n2), 0) = v :: nil
| getLevel(NODE (v, n1, n2), level) = getLevel(n1(), level - 1) @ getLevel(n2(), level - 1)
| getLevel(LEAF, level) = nil;
fun filter p LEAF = LEAF
| filter p (NODE(v, n1, n2)) =
if p v then NODE(v, fn() => filter p (n1()), fn() => filter p (n2()))
else LEAF;
val tree =
let
fun genTree(a) = NODE(a, fn() => genTree(a*2), fn() => genTree(a*2 + 1))
in
NODE(1, fn() => genTree(2), fn() => genTree(3))
end
fun printList (x::xs) = (print ((Int.toString x) ^ " "); printList xs)
| printList nil = ()
fun main(level_arg) =
let
val tree_example = filter (fn x => x mod 3 <> 0) tree
in
printList(getLevel(tree_example, level_arg))
end