Created
March 7, 2012 01:41
-
-
Save diegopacheco/1990342 to your computer and use it in GitHub Desktop.
Haskell Monad state that Works! GHCI 7.0.4 = 2012
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad.State | |
import Control.Monad | |
data FibState = F {previous, current :: Integer} | |
fibState0 = F {previous = 1, current = 0} | |
currentFib :: State FibState Integer | |
currentFib = gets current | |
nextFib :: State FibState Integer | |
nextFib = do | |
F p c <- get | |
let n = p+c | |
put (F c n) | |
return n | |
getNFibs :: Int -> State FibState [Integer] | |
getNFibs k = replicateM k nextFib | |
main :: IO () | |
main = print $ evalState (liftM2 (:) currentFib (getNFibs 5) ) fibState0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ ghci
Prelude>:load monad-state.hs
Prelude> main