Skip to content

Instantly share code, notes, and snippets.

@kayhide
Last active October 30, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayhide/f0f802303da6e0f1d1a154704e99222c to your computer and use it in GitHub Desktop.
Save kayhide/f0f802303da6e0f1d1a154704e99222c to your computer and use it in GitHub Desktop.
import Control.Monad
import Control.Monad.State
type CounterState = State Int
fivesWithCounter :: CounterState [Int]
fivesWithCounter = do
modify (+1)
fives <- fivesWithCounter
i <- get
return $ i:fives
main :: IO ()
main = print $ evalState take10 0
where take10 = do
fives <- fivesWithCounter
return $ take 10 fives
import Control.Monad
import Control.Monad.State
type CounterState = State Int
fivesWithCounter :: CounterState [Int]
fivesWithCounter = do
modify (+1)
(:) <$> get <*> fivesWithCounter
main :: IO ()
main = print $ evalState (take 10 <$> fivesWithCounter) 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment