caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] yet another question on lazy lists
@ 2002-07-06  1:48 Michael Vanier
  2002-07-06 15:26 ` Pierre Weis
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Vanier @ 2002-07-06  1:48 UTC (permalink / raw)
  To: caml-list


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-07-08  0:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-06  1:48 [Caml-list] yet another question on lazy lists Michael Vanier
2002-07-06 15:26 ` Pierre Weis
2002-07-06 18:22   ` William Lovas
2002-07-07  0:26     ` Michael Vanier
2002-07-08  0:33       ` John Max Skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).