tag:blogger.com,1999:blog-21500237.post5435344542269252872..comments2023-04-29T04:08:32.247-04:00Comments on Praise, Curse, and Recurse: The Polar Game in Haskell, Day 5 1/2: Refactoring with a MonadPaul R. Pottshttp://www.blogger.com/profile/04401509483200614806noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-21500237.post-35624580389119801462013-07-12T05:18:34.548-04:002013-07-12T05:18:34.548-04:00I always felt the State monad was a bit like cheat...I always felt the State monad was a bit like cheating. "Oh we're in a language that doesn't have mutable state, let's just pretend that we have." You might as well run in IO and use actual mutables, says the part of my brain that conveniently ignores the real differences between pure monads and actual effects for the sake of being able to make dramatic statements.<br /><br />I never figured out Writer, but that was probably because I didn't need to.<br /><br />The last serious thing I wrote that used State was for my BSc dissertation and State seemed highly appropriate at the time because I was writing a compiler and needed to carry a symbol table and other such information around and State made that very convenient.<br /><br />Thus, one then presumes that using State to model, well, game state, makes a lot of sense at some level, even if it's not perhaps the most... hmm... Haskelly thing to do. Whatever 'Haskelly' means.Matt Waltonhttps://www.blogger.com/profile/04683084278575333885noreply@blogger.comtag:blogger.com,1999:blog-21500237.post-49463380580629880772013-07-12T00:57:40.473-04:002013-07-12T00:57:40.473-04:00Jeff, State was the first one I tried to understan...Jeff, State was the first one I tried to understand after how to use the basic IO monad and I had a lot of trouble. This one seems easier! I am going over functors and monoids in _Learn You a Haskell_. I'm thinking on tricks that would show the progress of the ScoreTracker better on the command line. Please keep me posted with what you learn about State. I'm getting there -- being able just about understand how Writer works was very encouraging!Paul R. Pottshttps://www.blogger.com/profile/04401509483200614806noreply@blogger.comtag:blogger.com,1999:blog-21500237.post-5943065587065957362013-07-12T00:26:20.691-04:002013-07-12T00:26:20.691-04:00Good luck with the job search. I hope something p...Good luck with the job search. I hope something pans out soon.<br /><br />It occurs to me that I was a bit too clever in my code. When people talk about the Maybe monad, they usually talk about "Maybe a", where "a" is something else. Writer is like that, but it takes two things: the monoid, and the "something else" like what Maybe takes. So, defining ScoreTracker might make more sense defined like this:<br /><br />type ScoreTracker a = Writer (Sum Int) a<br /><br />That corresponds more closely to the "Maybe a" stuff that other tutorials talk about.<br /><br />I think you got there eventually, but it's worth keeping consistent, and maybe someone else won't make the leap.<br /><br />Ultimately, I think using runWriter is the best way to get the result at the ghci command line. In theory, we could use "instance" syntax to make ScoreTracker an instance of Show, but I couldn't get it to work, and for just doodling at the command line the runWriter form works fine.<br /><br />I'm actually teaching myself the State monad now. It's got its own level of weird beyond all the monadic goodness. I think we can make Writer work for the heart counter, too, but you mentioning "state" made me think of how to do this with the State monad. But that just makes my brain hurt, so we'll skip it for now. :-)Jeff Licquiahttps://www.blogger.com/profile/02635921619274584156noreply@blogger.com