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: -- (:)
From: Loup Vaillant <loup.vaillant@g...>
Subject: Re: [Caml-list] Help me find this pdf
2007/10/19, Brian Hurt <>:
> The only thing missing is some syntactic sugar to make the above pattern
> matching nicer- 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.

(Disclaimer: I suspect I'm a bit off-topic)
If we really want to, we can delay the evaluation at all costs, by
delaying even the car of the Cons cells

type 'a node_t =
    | Empty
    | Cons of 'a Lazy.t * 'a lazylist
and 'a lazylist = 'a node_t Lazy.t

For instance, when figuring out if the list is empty or not, the first
element isn't forced. You can even force the second element of the
list without forcing the first:

match Lazy.force zlst with
  | Cons(_, tl) ->
    match Lazy.force tl with
      | Cons(hd, _) ->
        let head = Lazy.force hd in

Or, assuming I can get away with unsafe code:

let hd zlst = match Lazy.force zlst with
  | Cons(h, _) = h
let tl zlst = match Lazy.force zlst with
  | Cons(_, t) = t

(* and finally *)
match Lazy.force hd (tl zlst)
with [...]

Well, I think this is overdoing laziness: more often than not, the
rest of a lazy list depends on the value of it's head (e.g. the list
of all integers, of a Fibonacci list). In such cases, doubling the
cost of laziness is worth nothing.

Loup Vaillant