functional programming - Ocaml - Lazy.force -


is map function in:

type 'a stream = cons of 'a * 'a stream lazy.t  let rec ones = cons(1, lazy(ones));;  let rec map (f:'a -> 'b) (s:'a stream) : 'b stream =   match s   |cons(h,t) -> cons(f h, lazy (map f (lazy.force t)));; ;; 

correct? lazy.forcing make memoized?

yes, correct.

note there no sharing of computation when applying on regular/cyclic instead of infinite datastructure (as ones here). forcing n first elements of map succ ones still apply succ n times. (there research work on languages allow detect such form of regularity/cycles , make strict mapping on them terminate, see e.g. cocaml project.)