[Haskell-beginners] IO / State / Do Syntax

Christopher Howard christopher.howard at frigidcode.com
Mon Aug 27 12:27:22 CEST 2012

So, I'm still working with my resource tracker idea -- keeping images
stored and organized inside a resource tracker structure that I can pass
around to functions that need them. Let's say I want a function like so:

initResources :: IO ResourceTracker

The idea being that initResources loads the image files, stores them in
the resource tracker (RT) structure, and returns the RT. I know I can do
something like so:

initResources = do pic1 <- loadImage "someimage.png" -- IO function
		   pic2 <- loadImage "someimage2.png"
		   -- ... and so on ...
		   let rt = emptyResourceTracker in
		   let rt' = storeImage rt "pic1keyword" pic1 in
		   let rt'' = storeImage rt' "pic2keyword" pic2 in
		   -- ... and so on, until finally: ...

Obviously, all the let statements and apostrophes are undesirable. So,
presumably what I need is to being using the State monad, yes? (I must
confess I have only a vague understanding of the State monad, even after
reading several tutorials.) But in my initResources function, how do I
mix use of the IO and State do syntax, and still get what I want?

I think this has something to do with Monad transformers, but I'm even
less clear on how those work.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 554 bytes
Desc: OpenPGP digital signature
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120827/605a3eda/attachment.pgp>

More information about the Beginners mailing list