sml works

 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
datatype StringList
= CONS of string * (unit -> StringList)
| 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 concat (xq, 0) = ""
| concat (NIL, n) = ""
| concat (CONS (x,xf), n) = x ^ " " ^ concat (xf(),n-1)
fun binString 0 = ""
| binString x = binString(x div 2)^(Int.toString (x mod 2))
val bin =
let
fun genBin 0 = NIL
| genBin x = CONS(binString x, fn() => genBin(x+1))
in
CONS("1", fn() => genBin(2))
end
fun number0 (nil) = 0
| number0 (#"0"::s) = 1 + number0(s)
| number0 (#"1"::s) = number0(s)
fun number1 (nil) = 0
| number1 (#"1"::s) = 1 + number1(s)
| number1 (#"0"::s) = number1(s)
fun predicate "" = false
| predicate s = (number0(explode(s)) = number1(explode(s)))
fun main () = concat(filter (predicate) bin, 10)