Browse thread
Parameter evaluation order
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Fernando Alegre <fernando@c...> |
| Subject: | Re: [Caml-list] Re: Parameter evaluation order |
On Fri, Aug 26, 2005 at 03:36:31PM +0300, Ville-Pertti Keinonen wrote:
> 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.
Ignoring efficiency concerns, may I suggest that the correct way to
build lists is by appending elements, not prepending them:
# let append (element,list) = list @ [element];;
val append : 'a * 'a list -> 'a list = <fun>
# append('d', append('c', append('b', append('a', []))));;
- : char list = ['a'; 'b'; 'c'; 'd']
This will evaluate in the right order.
Fernando