<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/07/746ede35d7b862c4b263842d67508891"
  from="Michael Vanier &lt;mvanier@c...&gt;"
  author="Michael Vanier"
  date="2002-07-06T14:18:48"
  subject="[Caml-list] yet another question on lazy lists"
  prev="2002/07/1eefe810f7f0f0aa410a2eab708b2234"
  next="2002/07/061b98c60a3aae3e13a775ccfea73028"
  next-in-thread="2002/07/efd8bf1158b6562718435420a02fd7f2"
  prev-thread="2002/07/1eefe810f7f0f0aa410a2eab708b2234"
  next-thread="2002/07/061b98c60a3aae3e13a775ccfea73028"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/746ede35d7b862c4b263842d67508891"
  from="Michael Vanier &lt;mvanier@c...&gt;"
  author="Michael Vanier"
  date="2002-07-06T14:18:48"
  subject="[Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/efd8bf1158b6562718435420a02fd7f2"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2002-07-06T15:26:37"
  subject="Re: [Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/d1b9c9518820db66620939f4ea73e1a4"
  from="William Lovas &lt;wlovas@s...&gt;"
  author="William Lovas"
  date="2002-07-06T18:22:57"
  subject="Re: [Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/f3f66088ad1575a8c6b10b09ffdfcded"
  from="Michael Vanier &lt;mvanier@c...&gt;"
  author="Michael Vanier"
  date="2002-07-07T09:20:21"
  subject="Re: [Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/99c5bb60f441d6e61974021204d72812"
  from="John Max Skaller &lt;skaller@o...&gt;"
  author="John Max Skaller"
  date="2002-07-08T00:34:06"
  subject="Re: [Caml-list] yet another question on lazy lists">
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>

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 -&gt; invalid_arg "stream_hd"
  | Cons (x, y) -&gt; x

let stream_tl s =
  match s with
    Nil -&gt; invalid_arg "stream_tl"
  | Cons (x, y) -&gt; Lazy.force y

let rec stream_map2 proc s1 s2 =
  match (s1, s2) with
    (Cons (x1, y1), Cons (x2, y2)) -&gt;
      Cons ((proc x1 x2), lazy (stream_map2 proc 
                                  (Lazy.force y1) 
                                  (Lazy.force y2)))
  | _ -&gt; 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

</contents>

</message>

