Skip to content

Instantly share code, notes, and snippets.

@notyy
Created May 29, 2012 14:55
Show Gist options
  • Save notyy/2828885 to your computer and use it in GitHub Desktop.
Save notyy/2828885 to your computer and use it in GitHub Desktop.
Writer with State
import Control.Monad
import Control.Monad.State
import Control.Monad.Writer
type User = String
type Log = [String]
login :: String -> State [User] ()
login u = do
modify (u:)
doSomeLogin :: State [User] ()
doSomeLogin = do
login "notyy"
login "bad guy"
login "kia"
loginWL :: String -> Writer Log ()
loginWL u = do
tell $ [u ++ " logged in"]
doSomeLoginWL = do
loginWL "notyy"
loginWL "bad guy"
loginWL "kia"
------------------------------can runState or runWriter, but how to combine them together?
*Main> runState doSomeLogin []
((),["kia","bad guy","notyy"])
*Main> runWriter doSomeLoginWL
((),["notyy logged in","bad guy logged in","kia logged in"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment