Date: Thu, 12 Aug 1999 13:25:53 +0200
From: Andreas Rossberg <firstname.lastname@example.org>
To: John Skaller <email@example.com>
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.
-- Andreas Rossberg, firstname.lastname@example.org
:: be declarative. be functional. just be. ::
This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:24 MET