Jan Brosius wrote:

> Doesn't haskell 98 allow in place updating e.g; for records?

Semantically yes, pragmatically no.

What this means is, it gives you what *looks* like in-place update (you 
don't have to write the copying yourself), but in practice it is 
usually implemented as copying... a new record is created with all 
fields the same except for the one you changed.

How can you tell?

Look at the following program:

data Foo = Foo { a :: Int, b :: String }

instance Show Foo where
  show f = show (a f) ++ " " ++ show (b f)

foo = Foo { a = 99, b = "green bottles\n" }

main = do print foo
          let bar = foo { a = 98 }
          print bar

Obviously, this displays

99 green bottles
98 green bottles

on stdout.  It *looks* like you've updated the `a' field of foo.  But 
in fact, foo is still there, untouched: you could have written

main = do print foo
          let bar = foo { a = 98 }
          print bar
          print foo

and got

99 green bottles
98 green bottles
99 green bottles

Of course, in the first program above, a clever compiler could figure 
out you never use foo again and implement the update by true 
update-in-place, but this is quite hard for the compiler to do.  
Usually you give it a hand by saying `I will use this datum in a 
single-threaded manner' by putting it inside the ST or IO monads.  This 
makes your code look much more imperative, though.

> Jan

HTH.

--KW 8-)
-- 
: Keith Wansbrough, MSc, BSc(Hons) (Auckland) ------------------------:
: PhD Student, Computer Laboratory, University of Cambridge, England. :
:  (and recently of the University of Glasgow, Scotland. [><] )       :
: Native of Antipodean Auckland, New Zealand: 174d47' E, 36d55' S.    :
: http://www.cl.cam.ac.uk/users/kw217/  mailto:[EMAIL PROTECTED]     :
:---------------------------------------------------------------------:

-- 
: Keith Wansbrough, MSc, BSc(Hons) (Auckland) ------------------------:
: PhD Student, Computer Laboratory, University of Cambridge, England. :
:  (and recently of the University of Glasgow, Scotland. [><] )       :
: Native of Antipodean Auckland, New Zealand: 174d47' E, 36d55' S.    :
: http://www.cl.cam.ac.uk/users/kw217/  mailto:[EMAIL PROTECTED]     :
:---------------------------------------------------------------------:




Reply via email to