English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2005-08-26 (12:36)
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.