Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Created March 7, 2012 01:41
Show Gist options
  • Save diegopacheco/1990342 to your computer and use it in GitHub Desktop.
Save diegopacheco/1990342 to your computer and use it in GitHub Desktop.
Haskell Monad state that Works! GHCI 7.0.4 = 2012
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
@diegopacheco
Copy link
Author

$ ghci
Prelude>:load monad-state.hs
Prelude> main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment