(************************************************************ lib_sequence.ml Created : Fri Feb 22 00:42:54 2002 Last modified: Wed Feb 27 23:46:34 2002 Compile: make # ************************************************************) exception Empty open Lazy type 'a delayed = 'a Lazy.status ref type 'a seq = Nil | Sequence of ('a * ('a seq) delayed) let head = function Sequence (a,_) -> a | _ -> raise Empty let tail = function Sequence (_,b) -> force b | _ -> raise Empty let nil = Nil let is_empty = function Nil -> true | _ -> false let cons a b = Sequence(a,b) let rec take stream n = if n = 0 then [] else match stream with Sequence (a,b) -> a::(take (force b) (n-1)) | _ -> raise Empty let rec nth n stream = if n = 0 then head stream else nth (n-1) (tail stream)