(* this is stolen from Harper's notes (p. 36), but I have generalized it. In my version you give makestream an initial value and a successor function and it returns the corresponding stream *) abstype 'a stream = Stream of unit -> ('a * 'a stream) with fun makestream (first,succ) = let fun step n = Stream (fn () => (n, step (succ n))) in step first end fun next(Stream f) = f() end; (* Example use #1 (natural numbers): val nats = makestream (0, fn n => n+1) ; val (a,nats) = next nats ; val (b,nats) = next nats ; *) (* Example use #2 (powers of 2): val pow2 = makestream (1, fn n => 2*n) ; val (a,pow2) = next pow2 ; val (b,pow2) = next pow2 ; val (c,pow2) = next pow2 ; *)