Skip to content

Instantly share code, notes, and snippets.

@gdevanla
Created December 3, 2016 17:46
Show Gist options
  • Save gdevanla/c4566f170207451199c4d9a84a08feec to your computer and use it in GitHub Desktop.
Save gdevanla/c4566f170207451199c4d9a84a08feec to your computer and use it in GitHub Desktop.
-- sequence a state monad
import Control.Monad.Trans.State
-- Example 1
let y = do { z <- get; let a = (Prelude.last z) + 1 in put (z Prelude.++ [a]);return $ (Prelude.last z) + 1} :: State [Int] Int
runState (sequenceA [y,y,y]) $ [1]
-- Example: 2
type X = State (M.Map String Int) Int
let y s s1 = do { z <- get; let a = z M.! s + 1 in put (M.insert s1 a z) ; let a = z M.! s + 1 in return $ a } :: X
runState (sequenceA [(y "a" "b"), (y "b" "c")]) $ M.fromList [("a", 10)]
-- > ([11,12],fromList [("a",10),("b",11),("c",12)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment