Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] real lazy?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-08-21 (15:38)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] real lazy?
Francis Dupont <> writes:

> 3- streams (aka lazy lists)
>  open Lazy
>  (* to get type t and function force *)

you could use Lazy.force and Lazy.t

>  type 'a cell = Nil | Cons of 'a * 'a stream
>  and 'a stream = 'a cell Lazy.t
>  but this type is not really polymorphic:
>  «lazy Nil» has type «'_a cell Lazy.status ref» i.e. «'_a stream»
>  not «'a stream» as it should be!

it's not such a trouble. You could write a little function :

let nil () = lazy Nil

and create a new empty stream each time you need it. 
You could also use ocaml stream : (of type Stream.t, see the doc of
the modules, and thing about ocaml extension in the manual.)  It is
not exactly the same, but it's quite useful. 

>  So in place of a module Stream I had to write a functor Stream
>  (with «sig type t end» as the argument signature) in order to
>  fix the type of elements of streams. Argh!!

i don't believe that a functor is such a trouble. and you can easily
make otherwise.

>  The real purpose of streams is to write:
>  let map f =
>   let rec mapf s = 
>    lazy begin
>     match force s with
>      | Nil -> Nil
>      | Cons(x,r) -> Cons(f x,mapf r)
>    end
>   in mapf
>  let rec nat = lazy (Cons(0,map succ nat))
>  and so on...

let nat =
   let rec aux n =
     lazy (Cons(n, aux (n+1))) in
   aux 0

is much better.

Rémi Vanicat
Bug reports:  FAQ:
To unsubscribe, mail  Archives: