From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA00675; Thu, 4 Jul 2002 17:14:25 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA00777 for ; Thu, 4 Jul 2002 17:14:24 +0200 (MET DST) Received: from beaune.inria.fr (beaune.inria.fr [128.93.8.3]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g64FENP16739 for ; Thu, 4 Jul 2002 17:14:23 +0200 (MET DST) Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id RAA0000002698; Thu, 4 Jul 2002 17:14:13 +0200 (MET DST) Date: Thu, 4 Jul 2002 17:14:13 +0200 (MET DST) From: Damien Doligez Message-Id: <200207041514.RAA0000002698@beaune.inria.fr> To: caml-list@inria.fr, mvanier@cs.caltech.edu Subject: Re: [Caml-list] more on lazy lists Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: Michael Vanier >Now I understand why references are used internally for lazily evaluated >values; it's for memoization i.e. so that a particular value doesn't have >to be recomputed after it's been computed once. This is the essence of lazy evaluation. > I'm having an odd problem, though; consider this code: [...] Your "stream_map2" function is not lazy enough: it will unnecessarily force the tail of the stream. You need to write it as follows: let rec stream_map2 proc s1 s2 = match (s1, s2) with (Cons (x1, y1), Cons (x2, y2)) -> Cons ((proc x1 x2), lazy (stream_map2 proc (Lazy.force y1) (Lazy.force y2))) | _ -> raise Invalid_operation Note that the calls to Lazy.force are inside the argument of lazy. There is the same problem in your "take" function. >let integers = > let rec ints () = > Cons (1, lazy (add_streams ones (ints ()))) > in > ints () Now it works, but it is very inefficient (quadratic complexity) because you rebuild a new "ints" stream at each call to add_streams. Better to do it this way: let rec integers = Cons (1, lazy (add_streams ones integers));; -- Damien ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners