Re: Constructor/reference bug?

From: Andreas Rossberg (rossberg@ps.uni-sb.de)
Date: Thu Aug 12 1999 - 13:25:53 MET DST


Date: Thu, 12 Aug 1999 13:25:53 +0200
From: Andreas Rossberg <rossberg@ps.uni-sb.de>
To: John Skaller <skaller@maxtal.com.au>
Subject: Re: Constructor/reference bug?

John Skaller wrote:
>
> 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.

Evaluation order of application is not specified in OCaml: arguments may
be evaluated in any order (in fact, byte code does it right-to-left,
native code left-to-right, if I remember correctly). That seems to be
what's happening here: (f t), the second argument to ::, is evaluated
before (CTRL !lineno). Using let eliminates this problem.

HTH,

        - Andreas

-- 
Andreas Rossberg, rossberg@ps.uni-sb.de

:: be declarative. be functional. just be. ::



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