English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] yet another question on lazy lists
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-07-06 (18:22)
From: William Lovas <wlovas@s...>
Subject: Re: [Caml-list] yet another question on lazy lists
On Sat, Jul 06, 2002 at 05:26:33PM +0200, Pierre Weis wrote:
> >  Does this mean
> > that my "stream_cons" function is useless?
> >
> > Thanks in advance,
> > 
> > Mike
> No. It means you cannot always use it in place of a constructor
> application. Note that a similar phenomenon occurs for pattern
> matching: constructors are mandatory, you cannot subtitute them by
> equivalent function calls!

Actually, i think stream_cons may be useless, due to OCaml's eager
evaluation strategy.  If you try to delegate the recursion to a function
call, the following happens:

    # let rec constant n = stream_cons n (constant n);;
    val constant : 'a -> 'a stream = <fun>
    # constant 1;;
    Stack overflow during evaluation (looping recursion?).

... but the constructor still works:

    # let rec constant n = Cons (n, lazy (constant n));;
    val constant : 'a -> 'a stream = <fun>
    # constant 1;;
    - : int stream = Cons (1, {contents = Lazy.Delayed <fun>})

The second argument needs to be treated lazily (which it is, explicitly, 
using the Cons constructor call), but stream_cons will never achieve that,
unless i'm missing something.


P.S. Michael, why not call your type something like `lazy_list', instead of
`stream'?  The latter conjures up images of stateful Stream.t's, for me at
least.  Just a thought!
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