caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Michael Vanier <mvanier@cs.caltech.edu>
To: caml-list@inria.fr
Subject: [Caml-list] yet another question on lazy lists
Date: Fri, 5 Jul 2002 18:48:35 -0700	[thread overview]
Message-ID: <200207060148.g661mZp06460@orchestra.cs.caltech.edu> (raw)


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


             reply	other threads:[~2002-07-06 14:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-06  1:48 Michael Vanier [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200207060148.g661mZp06460@orchestra.cs.caltech.edu \
    --to=mvanier@cs.caltech.edu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).