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
Help me find this pdf
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-10-19 (23:17)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Help me find this pdf
On Friday 19 October 2007 14:00:38 you wrote:
> Bad definition of a lazy list- the first element always has to be
> forced.  Using my definition from above:
> let rec f zlst = lazy (
>     match Lazy.force zlst with
>     | Empty -> Empty
>     | Cons (None, xs) -> Cons (None, f xs)
>     | Cons (Some x, xs) as t ->
>        match Lazy.force xs with
>           | Empty -> t
>           | Cons(None, ys) -> Cons(Some x, lazy (Cons(None, f ys)))
>           | Cons(Some y, ys) -> Cons (Some (x @ y), f ys)
> );;
> Not quite as pretty, I'll admit.

Ugly as hell in the general case: you're basically stuck writing Lisp.

> But it works.


> And (modulo laziness around the options and appending) is the same as what
> Haskell would do. 


> The only thing missing is some syntactic sugar to make the above pattern
> matching nicer-

I do not class pattern matching or laziness as "syntax". This would be 
non-trivial to implement.

> computationally, the same values will need to be 
> forced.  If you're arguing in favor of the syntactic sugar, I'm
> sympathetic.  If you're arguing that the compiler could somehow avoid
> forcing the same values, I don't see how.

OCaml's pattern match compiler could be productively extended to generate that 
code for you, IMHO. Implementing this as a camlp4 macro might be quite 

Dr Jon D Harrop, Flying Frog Consultancy Ltd.