datatype IntList CONS of int unit IntList NIL fun filter NIL NIL filte

 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
datatype IntList
= CONS of int * (unit -> IntList)
| NIL
fun filter p NIL = NIL
| filter p (CONS (x,xf)) =
if p x then CONS (x, fn() => filter p (xf()))
else filter p (xf())
fun take (xq, 0) = nil
| take (NIL, n) = nil
| take (CONS (x,xf), n) = x :: take (xf(),n-1)
val fib =
let
fun genFib (a,b) = CONS(b, fn() => genFib(b,a+b))
in
CONS(1, fn() => genFib(1,1))
end
(* Test *)
fun printList (x::xs) = (print ((Int.toString x) ^ " "); printList xs)
| printList nil = ()
fun main () =
let
val oddFib = filter (fn x => x mod 2 = 0) fib
in
printList (take (oddFib,10))
end