From: Damien Doligez <damien.doligez@inria.fr>
To: caml-list@inria.fr, mvanier@cs.caltech.edu
Subject: Re: [Caml-list] more on lazy lists
Date: Thu, 4 Jul 2002 17:14:13 +0200 (MET DST) [thread overview]
Message-ID: <200207041514.RAA0000002698@beaune.inria.fr> (raw)
From: Michael Vanier <mvanier@cs.caltech.edu>
>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
next reply other threads:[~2002-07-04 15:14 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-07-04 15:14 Damien Doligez [this message]
-- strict thread matches above, loose matches on Subject: below --
2002-07-04 3:51 Michael Vanier
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=200207041514.RAA0000002698@beaune.inria.fr \
--to=damien.doligez@inria.fr \
--cc=caml-list@inria.fr \
--cc=mvanier@cs.caltech.edu \
/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