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 QAA05426; Sat, 6 Jul 2002 16:18:48 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA03754 for caml-list@pauillac.inria.fr; Sat, 6 Jul 2002 16:18:47 +0200 (MET DST) 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 DAA31897 for ; Sat, 6 Jul 2002 03:48:38 +0200 (MET DST) Received: from obento.cs.caltech.edu (obento.cs.caltech.edu [131.215.44.101]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g661mbf13866 for ; Sat, 6 Jul 2002 03:48:37 +0200 (MET DST) Received: from orchestra.cs.caltech.edu (orchestra.cs.caltech.edu [131.215.44.20]) by obento.cs.caltech.edu (Postfix) with ESMTP id 29E584052 for ; Fri, 5 Jul 2002 18:48:35 -0700 (PDT) Received: (from mvanier@localhost) by orchestra.cs.caltech.edu (8.11.6/8.9.3) id g661mZp06460; Fri, 5 Jul 2002 18:48:35 -0700 Date: Fri, 5 Jul 2002 18:48:35 -0700 Message-Id: <200207060148.g661mZp06460@orchestra.cs.caltech.edu> X-Authentication-Warning: orchestra.cs.caltech.edu: mvanier set sender to mvanier@cs.caltech.edu using -f From: Michael Vanier To: caml-list@inria.fr Subject: [Caml-list] yet another question on lazy lists Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk First off, thanks very much to all of you who have helped me understand lazy evaluation in ocaml. Now for another question: I get a strange type error from the following code: (* "stream" means lazy lists for my purposes. *) type 'a stream = Nil | Cons of 'a * 'a stream Lazy.t let stream_cons x y = Cons (x, lazy y) let stream_hd s = match s with Nil -> invalid_arg "stream_hd" | Cons (x, y) -> x let stream_tl s = match s with Nil -> invalid_arg "stream_tl" | Cons (x, y) -> Lazy.force y 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))) | _ -> invalid_arg "stream_map2" let add_streams s1 s2 = stream_map2 (+) s1 s2 (* Generating fibonacci numbers. *) let rec fibs = Cons (0, lazy (Cons (1, lazy (add_streams (stream_tl fibs) fibs)))) Thus far, everything works properly. But when I try to convert the "Cons" expressions into "stream_cons" function calls, I get a weird type error: let rec fibs2 = stream_cons 0 (stream_cons 1 (add_streams (stream_tl fibs2) fibs2)) # let rec fibs2 = stream_cons 0 (stream_cons 1 (add_streams (stream_tl fibs2) fibs2)) ------------------------------------------------------------------- ;; This kind of expression is not allowed as right-hand side of `let rec' What does this mean? My best guess is that ocaml sees that you're defining a value in terms of itself (like e.g. "let rec a = 2 * a") and won't allow it, but that doesn't explain why the lazy version works. Does this mean that my "stream_cons" function is useless? Thanks in advance, Mike ------------------- 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