Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Constructor/reference bug?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <mottl@m...>
Subject: Re: Constructor/reference bug?
> 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?

No, but OCaml evaluates all kind of expressions right-to-left which is
probably not very intuitive for people who are used to read from left
to right...

Regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl