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
Constructor/reference bug?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1999-08-12 (14:42)
From: Nicolas Ollinger <nollinge@e...>
Subject: Re: Constructor/reference bug?
On Wed, 11 Aug 1999, John Skaller wrote:

> I have some code like:


> 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

It calls f before computing !blob.

In hope this will help you,