datatype 'a seq = Nil | Cons of 'a * (unit -> 'a seq); (* sml interpreter warns about tail, hd, and tl having patterns which do not 'cover' the domain of their parameters *) fun hd(Cons(x,_)) = x; (* type of tail is: fn: 'a seq -> 'a seq *) fun tail(Cons(_,xf)) = xf(); (* type of tl is: fn: 'a seq -> unit -> 'a seq *) fun tl(Cons(_,xf))= xf; (* need fn() to show sml interpreter that this is a function with no arguments*) fun from k = Cons(k, fn()=> from (k+1)); (* builds stream *) fun takeq (0,xq) = [] | (*enables selection of finite #*) takeq (n ,Nil) = []| (*of elements of stream*) takeq (n, Cons(x,xf)) = x::takeq(n-1, xf()); fun filtereq pred Nil = Nil | (*to filter a stream*) filtereq pred (Cons(x,xf)) = if pred x then Cons(x, fn()=>filtereq pred (xf())) else filtereq pred (xf()); (*deletes multiples of p from a sequence *) fun sift p = filtereq (fn n=> n mod p <> 0); (*repeatedly sifts a seq*) fun sieve (Cons (p,nf)) = Cons (p, fn()=>sieve (sift p (nf())));