Created
November 16, 2012 12:35
-
-
Save freizl/4086996 to your computer and use it in GitHub Desktop.
Hello State Monad
This file contains 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
module Main where | |
import Control.Monad.Trans.State.Lazy | |
-- | The state | |
type S = String | |
sayHello :: S -> S | |
sayHello = (++ "Hello, ") | |
sayWorld :: S -> S | |
sayWorld = (++ "World") | |
-- | Play with just State | |
taskOne :: State S a -> S | |
taskOne = flip execState "" | |
. withState sayHello | |
. withState sayWorld | |
initState :: Monad m => StateT S m () | |
initState = StateT (\s -> return ((), s)) | |
-- | Play with State Monad | |
taskTwo :: Monad m => StateT S m S | |
taskTwo = modify sayHello | |
>> modify sayWorld | |
>> get | |
main = do | |
let s1 = taskOne initState | |
print ("Task one: " ++ s1) | |
(s2, _) <- runStateT taskTwo "" | |
print ("Task two: " ++ s2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment