tag:blogger.com,1999:blog-22587889.post7365584747034896556..comments2024-02-11T13:21:47.930+05:30Comments on Ruminations of a Programmer: Fizzbuzz and the State MonadAnonymoushttp://www.blogger.com/profile/01613713587074301135noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-22587889.post-48123620573746358392014-08-28T19:50:44.694+05:302014-08-28T19:50:44.694+05:30instead of
(_, state) = runState ...
you can u...instead of <br /><br />(_, state) = runState ... <br /><br />you can use the execState function<br /><br />-- runState :: State s a -> s -> (a, s)<br />-- execState :: State s a -> s -> s<br /><br />state = execState ...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-22587889.post-71025633339847690262009-02-07T02:01:00.000+05:302009-02-07T02:01:00.000+05:30Actually, here's one silly way of fizzubzzing ...Actually, here's one silly way of fizzubzzing in the state monad:<BR/><BR/>import Control.Monad.State<BR/><BR/>next :: State (Int,Int,Int) String<BR/>next = do (a,b,c) <- get<BR/> put ((a+1) `mod` 3, (b+1) `mod` 5, c+1)<BR/> return $ case (a,b) of<BR/> (0,0) -> "fizzbuzz"<BR/> (0,_) -> "fizz"<BR/> (_,0) -> "buzz"<BR/> _ -> show c<BR/><BR/>main = print . take 100 . fst $ runState (sequence $ repeat next) (1,1,1)Arnar Birgissonhttps://www.blogger.com/profile/12073820949049315334noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-75563427731039959662009-01-20T13:56:00.000+05:302009-01-20T13:56:00.000+05:30@Arnar Birgisson:Thanks for the input. I was tryin...<I>@Arnar Birgisson:</I><BR/><BR/>Thanks for the input. I was trying to understand State monad with that example and had not yet gone through the Writer monad. Now, after going through the Writer monad, I discovered the same solution that u have mentioned. This, I guess, is the power of Haskell, that u can address every problem in multiple ways, but choose the solution which feels most succinct and elegant.Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-82430201817977345792009-01-20T08:11:00.000+05:302009-01-20T08:11:00.000+05:30But why would you do that instead of just using IO...But why would you do that instead of just using IO directly? Like this:<BR/><BR/>main = sequence_ $ map (putStrLn . fizzbuzz) [1..100]<BR/><BR/>Mind you, I think your example is a good demonstration of how State works; but it'd be nice to see something that explains why to use it.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-22587889.post-88953514593358708702009-01-20T00:05:00.000+05:302009-01-20T00:05:00.000+05:30By mapM_ing your collect function over the state m...By mapM_ing your collect function over the state monad, you are doing similar things to what the Writer monad does directly.<BR/><BR/>import Control.Monad.Writer<BR/><BR/>fizzbuzz n <BR/> | n `rem` 3 == 0 && n `rem` 5 == 0 = "fizzbuzz"<BR/> | n `rem` 3 == 0 = "fizz"<BR/> | n `rem` 5 == 0 = "buzz"<BR/> | otherwise = show n<BR/><BR/>fizzbuzzW :: Integer -> Writer [String] ()<BR/>fizzbuzzW = tell . return . fizzbuzz<BR/><BR/>main = print $ snd . runWriter $ mapM_ fizzbuzzW [1..100]<BR/><BR/><BR/>(please fix the indentation if blogger messes it up)Arnar Birgissonhttps://www.blogger.com/profile/12073820949049315334noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-81371838221507100522009-01-19T23:55:00.000+05:302009-01-19T23:55:00.000+05:30Yes, I am a Haskell newbie trying to explore the l...Yes, I am a Haskell newbie trying to explore the language through Real World Haskell. Most of the examples in the book are truly helpful. However, I found the example of the State monad slightly opaque mainly because of the fact that I am not (yet) familiar with the various classes like Random, RandomGen, StdGen etc. And I did not want to do a depth first exploration of those topics while I am trying to understand something else. OTOH fizzbuzz is a problem which can be suitably molded to demonstrate various idioms of the language.Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-62898559076762437542009-01-19T23:32:00.000+05:302009-01-19T23:32:00.000+05:30Are you reading/have read Real World Haskell by an...Are you reading/have read Real World Haskell by any chance? Up until I started reading RWH it was hard for me to even find examples like yous here. Thanks for the great example.Anonymousnoreply@blogger.com