[Haskell-beginners] State Monad

Thomas Jakway tjakway at nyu.edu
Fri Apr 17 19:25:52 UTC 2015


I'm having some trouble using the state monad with a random number 
generator.

I'm trying to follow the LYAH example 
(http://learnyouahaskell.com/for-a-few-monads-more) but apparently the 
state monad API has changed, so I'm using Control.Monad.Trans.State.Lazy 
(state monad from the transformers package).  Side question: is using 
transformers a good idea?  I've heard some people complain about mtl's 
performance, others don't seem to care.  I'm far too new to be able to 
judge anything (does it even make sense to compare transformers vs. 
mtl?) but if one has overtaken the other I'd rather use that.

Here's what I currently have:
import Control.Monad.Trans.State.Lazy
type GeneratorState = State StdGen

genThree :: Int -> GeneratorState Int
genThree listMax = do --highest index in the list
         let listMin = 0 :: Int --lowest index in the list
         generator <- get
         let (generatedMin, state) = randomR (listMin, listMax) generator
         return generatedMin

Although it typechecks it doesn't seem like an improvement over just 
using StdGen by itself.

What I think I should have:
genThree :: Int -> GeneratorState Int
genThree listMax = do --highest index in the list
         let listMin = 0 :: Int --lowest index in the list
         generatedMin <- state randomR (listMin, listMax)
         return generatedMin
*
*I feel like I really botched this--what am I missing?
*
*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150417/9201074c/attachment.html>


More information about the Beginners mailing list