From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA18632 for caml-redistribution; Thu, 12 Aug 1999 16:42:54 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA12015 for ; Thu, 12 Aug 1999 14:05:42 +0200 (MET DST) Received: from tobago.inria.fr (tobago.inria.fr [128.93.8.21]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id OAA11927; Thu, 12 Aug 1999 14:05:35 +0200 (MET DST) Received: (from doligez@localhost) by tobago.inria.fr (8.6.10/8.6.6) id OAA22637; Thu, 12 Aug 1999 14:05:35 +0200 Date: Thu, 12 Aug 1999 14:05:35 +0200 From: Damien Doligez Message-Id: <199908121205.OAA22637@tobago.inria.fr> To: caml-list@inria.fr, skaller@maxtal.com.au Subject: Re: Constructor/reference bug? Sender: weis >From: John Skaller >let g x = >let lineno = ref (1,"") in >let rec f x' = match x' with >| NEWLINE p :: t -> lineno := p; f t >| COLON :: t -> CTRL !lineno :: f t >| h :: t -> h :: f t >| [] -> [] >in f x;; > >The code doesn't work as I expected: every >CTRL value refers to the same lineno, the last one. >If I change the COLON line to read: > >| COLON :: t -> let x = !lineno in CTRL x :: f t > >the code works as expected -- the CTRL refers to the >value of lineno at the time the last NEWLINE was processed. > >What's happening? Are coproduct constructors lazy? You have to be careful because f is a function with side effects. In the line | COLON :: t -> CTRL !lineno :: f t the subexpression "!lineno" is not guaranteed to be evaluated before "f t". The current implementation happens to evaluate "f t" first. Using "let x = !lineno in ..." is the right way to write your function. -- Damien