Version française
Home     About     Download     Resources     Contact us    
Browse thread
Parameter evaluation order
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Ville-Pertti Keinonen <will@e...>
Subject: Re: [Caml-list] Re: Parameter evaluation order
Christophe Raffalli wrote:

> Jon Harrop a écrit :

>> Semicolons are used in many places in OCaml's grammar. Would you 
>> expect the members of a list literal to be evaluated left-to-right, 
>> for example?

> yes I would ...

An OCaml list is built starting with the tail.  Left-to-right evaluation 
could end up using huge amounts of temporaries.

Consider an explicit implementation of lists:

type 'a list = Cons of 'a * 'a list | Nil

Now, you'd write the list [ a; b; c; d ] (where a, b, c and d could be 
complex expressions) as

Cons (a, Cons (b, Cons (c, (Cons (d, Nil)))))

You need to have Cons (d, Nil) before you can construct Cons (c, ...) 
etc.  It's the innermost expression, so evaluating it first makes sense 
in any sensible evaluation order.