[Haskell-beginners] Request for Another State Monad Example
Mike Sullivan
mbsullivan at gmail.com
Wed Sep 17 14:05:46 EDT 2008
Hi All,
As I'm sure all Haskell beginners do, I'm having a bit of a struggle
wrapping my head around all of the uses for monads. My current frustration
is trying to figure out how to use the state monad to attach some persistent
state between different calls to a function. I have two questions that I
would appreciate it if somebody could help me with.
The ubiquitous state monad example for Haskell tutorials seems to be a
random number generator, with a function like the following (from
http://www.haskell.org/all_about_monads/html/statemonad.html#example):
getAny :: (Random a) => State StdGen a
getAny = do g <- get
(x,g') <- return $ random g
put g'
return x
My first question is very basic, but here it goes: I see it everywhere, but
what does the "=>" signify? Specifically, in this example what does "(Random
a) =>" do in the type signature?
My second question is more of a request, I suppose. I think it would be
useful to get another example that does not have the added complications of
dealing with the Random package, and saves more than one piece of data as
state. How would one go about (for example) creating a Fibonacci sequence
generator that saves the last state, such that on each call it returns the
next number in the Fibonacci sequence?
Thank you,
Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080917/39f0badd/attachment.htm
More information about the Beginners
mailing list