Re: Constructor/reference bug?

From: Nicolas Ollinger (nollinge@ens-lyon.fr)
Date: Thu Aug 12 1999 - 12:51:32 MET DST


Date: Thu, 12 Aug 1999 12:51:32 +0200 (MET DST)
From: Nicolas Ollinger <nollinge@ens-lyon.fr>
To: John Skaller <skaller@maxtal.com.au>
Subject: Re: Constructor/reference bug?
In-Reply-To: <3.0.6.32.19990811180530.00985e40@mail.triode.net.au>

On Wed, 11 Aug 1999, John Skaller wrote:

> I have some code like:

(snip)

> What's happening? Are coproduct constructors lazy?
>
> [I tested some simpler samples by hand and my tests
> worked as expected, so I'm confused]

here is a simpler sample :

let blob = ref 0 in
let rec f = function
| true::t -> incr blob; f t
| false::t -> (!blob)::(f t)
| [] -> []
in f [true;false;false;false;true;true;false];;

the result is : [3; 3; 3; 3].

But this is not a bug... this is normal!
And of course if you write let tmp = !blob in tmp::(f t) the
result will be [1; 1; 1; 3].

You don't see why it is so because there is a mix of infix and prefix
operators there. But remember that the order of arguments evaluation is
not specified in Caml. And precisely, OCaml do it from right to left.
Then, the text:
(!blob)::(f t)

Is translated into something like:
let arg2 = f t
and arg1 = !blob in
arg1::arg2

It calls f before computing !blob.

In hope this will help you,
N.

--



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:24 MET