Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] real lazy?
Francis Dupont <Francis.Dupont@enst-bretagne.fr> 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
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr