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
[Caml-list] Wasn't O'Caml a functional language?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-09-24 (08:36)
From: Alessandro Baretta <alex@b...>
Subject: Re: [Caml-list] Re: Wasn't O'Caml a functional language?

Michaël Grünewald wrote:
> Alessandro Baretta <> writes:

>>Anyway, my main claim, although misdirected, in not entirely
>>faulty. Queue.transfer can be thought of as analogous to
>>List.append. When I write let list = list1 @ list2 I do not expect
>>side-effects on list1 or list2.
> Then you maybe should use Lists, why, if queues have the same behavior than
> lists, give them a different name ?

They don't have the same behavior, and are not supposed to 
have the same behaviour.

let l = [] in
let l = e1 :: l in
let l = e2 :: l in
let l = e3 :: l in
List.iter f l

is equivalent to
f e3; f e2; fe1

which is not what I need. On the other hand,
let q = Queue.create () in
let _ = Queue.add e1 q in
let _ = Queue.add e2 q in
let _ = Queue.add e3 q in
Queue.iter f q

is equivalent to
f e1; f e2; f e3

which is correct with respect to what I need. This is the 
reason for using Queues. I somehow expected this to be the 
only difference with respect to Lists, and did not suspect 
that some of the functions of the Queue module (other than 
the obvious add and take) had side-effects. I realize that 
"transfer" is a significantly different name than "append", 
and I should have known better than to use it without 
expecting side-effects, but, anyway, I was stumbled on this 
function. And, believe me, it took me quite a while to 
figure out in Ocamldebug/Epeire why in the world my program 
was doing what it was. So let me say, "Long live functional 
iterators which have no side-effects! Down with explicit 
handling of aliasing! " [ ... feel free to add here whatever 
political slogans you like best ;) ]

BTW, picking up on Pixel's comment, I don't really know 
whether Queues are any more efficient than 
lists-used-as-FIFO, although I would expect them to be. I am 
mostly interested in the conciseness of the API.
Queue.iter f q
is much more elegant and readable than the equivalent
List.iter f (List.rev l)

Of course, performance is also important, but the software 
I'm writing will have its bottleneck in IO anyway, so no 
there is no significant advantage to be had by optimising 
the algorithms, other than to increase the fraction of CPU 
idle time, which is already pretty high. Of course, in other 
contexts, performace would matter a lot more.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: