
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: code: -------- 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: code: -------- 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: ... rt'''''''''''''''''''''''''' -------- 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. -- frigidcode.com indicium.us

Hi you don't need the State monad for your problem. Standard functional composition is enough: ---8<--- initResources = let images = ["someimage.png", "someimage2.png"] keywords = ["pic1keyword", "pic2keyword"] in do pics <- mapM loadImage images let rt = foldr go emptyResourceTracker $ zip pics keywords -- use rt where go (pic, kw) rt = storeImage rt kw pic --->8--- Anyways, if you are interested in Monad transformers in general, go check out http://book.realworldhaskell.org/read/monad-transformers.html HTH Alex
participants (2)
-
Alexander Bernauer
-
Christopher Howard