Skip to content

Instantly share code, notes, and snippets.

@freizl
Created November 16, 2012 12:35
Show Gist options
  • Save freizl/4086996 to your computer and use it in GitHub Desktop.
Save freizl/4086996 to your computer and use it in GitHub Desktop.
Hello State Monad
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