Created
December 3, 2016 17:46
-
-
Save gdevanla/c4566f170207451199c4d9a84a08feec to your computer and use it in GitHub Desktop.
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
-- 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