[Haskell-cafe] Beginner: IORef constructor?

Udo Stenzel u.stenzel at web.de
Fri Dec 1 08:03:28 EST 2006

TJ wrote:
> ----------------------------------------------------------
> module Global where
> import Data.IORef
> theGlobalVariable = newIORef []
> testIt = do ref <- theGlobalVariable
>            original <- readIORef ref
>            print original
>            writeIORef ref [1,2,3]
>            new <- readIORef ref
>            print new
> ----------------------------------------------------------

Wrong.  You get a fresh new "variable" everytime you access

> I've got a lot to learn about Haskell...

Well, for starters:

- there are no variables in ordinary Haskell,
- there are variables in the ST and IO monads, but dragging IO
  everywhere is burdensome and you don't want to do that,
- you can probably fake global variables using 'unsafePerformIO', and
  you definitely don't want to mess with that (yet),
- you need to understand monads in general, the State monad, the ST
  monad and the IO monad, and in exactly this order.
Whatever you're trying to do right now, just forget that there are
variables in BASIC and do it without mutable state.

They laughed at Einstein.
They laughed at the Wright Brothers.
But they also laughed at Bozo the Clown.
	-- attributed to Carl Sagan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20061201/b525154b/attachment.bin

More information about the Haskell-Cafe mailing list