On 16-Mar-1999, Tim Bishop <[EMAIL PROTECTED]> wrote:
> I am relatively new to Haskell, and I'm using Hugs 1.4.
>
> My my source of programming is Java, with the odd bit of basic thrown in for
> good measure.
>
> Now one of the first things I notice about Haskell is that there don't seem
> to be variables in the same sense that there are in other programming
> languages. I want to be able to store a value, or a set of values for use
> through-out various functions.
>
> Any suggestions ?
Hugs and ghc provide the following, in module IOExts, as part of the
Hugs/ghc extension libraries:
data IORef a -- mutable variables containing values of type a
newIORef :: a -> IO (IORef a)
readIORef :: IORef a -> IO a
writeIORef :: IORef a -> a -> IO ()
instance Eq (IORef a)
These functions let you create, read, and write mutable variables,
so long as all the code that accesses these variables is in the IO Monad.
For example:
increment var = do
val <- readIORef var
writeIORef var (val + 1)
main = do
v1 <- newIORef 42
increment v1
v1_val <- readIORef v1
print v1_val
--
Fergus Henderson <[EMAIL PROTECTED]> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED] | -- the last words of T. S. Garp.