Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Using IORef (Oliver Charles)


----------------------------------------------------------------------

Message: 1
Date: Tue, 07 Jan 2014 11:47:20 +0000
From: Oliver Charles <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Using IORef
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

On 07/01/14 03:21, Brandon Allbery wrote:
> On Mon, Jan 6, 2014 at 10:15 PM, Courtney Robinson
> <[email protected] <mailto:[email protected]>> wrote:
>
>     On Tue, Jan 7, 2014 at 3:03 AM, Brandon Allbery
>     <[email protected] <mailto:[email protected]>> wrote:
>
>         behind 
>
>
>     Oh I see, thanks for the info. really helpful.
>     It brings about another question now.
>
>     How is newIORef meant to be used so that I only have a single IORef?
>
>
> The Haskell way is to carry such things implicitly in a State or
> Reader monad; since the IORef itself doesn't change, and you need the
> IO monad around anyway to actually use it, you would use a ReaderT
> IORef IO and then use ask >>= liftIO . readIORef to get the value and
> similar to write or modify it. (Commonly one uses a type or
> newtype+newtype deriving to make one's own monad combining them.)

You don't have to go that far though, if you want a 'global' IORef then
you simply make it at the top of your program and then pass it around to
anyone who needs access it to:

main :: IO ()
main = do
  counter <- newIORef 0
  doThingsWithCounter counter

doThingsWithCounter :: IORef Int -> IO ()
doThingsWithCounter counter = |atomicModifyIORef counter $\x ->
  lety =x +1in(y,y)

|So this gives you a way to have global variables, but without the pain
that they can bring in other languages.

The Reader monad stuff that Brandon suggests is a way to implicitly have
this IORef passed around everywhere, which can be useful if you have
deeply nested calls where only the children really need access to the
IORef - it saves you having to add a new parameter everywhere.

- ocharles
||
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140107/6871f212/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140107/6871f212/attachment-0001.sig>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 67, Issue 11
*****************************************

Reply via email to