lab UNIVERSITY

 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
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 N =
let
fun genN (a) = CONS(a+1, fn() => genN(a+1))
in
CONS(1, fn() => genN(1))
end
fun printList (x::xs) = (print ((Int.toString x) ^ " "); printList xs)
| printList nil = ()
fun pred(x) = IntListSet.numItems( IntListSet.addList(IntListSet.empty, map((fn x => (valOf(Int.fromString(x)) ))) (map((fn x => Char.toString(x))) (String.explode(Int.toString(x))) ) )) = 4
fun main () =
let
val result = filter pred N
in
printList (take (result,10))
end