Writer

Functions

instance Monad ((->) r) where  
    return x = \\_ -> x  
    h >>= f = \\w -> f (h w) w
import Control.Monad.Instances  
  
addStuff :: Int -> Int  
addStuff = do  
    a <- (*2)  
    b <- (+10)  
    return (a+b)

State

type Stack = [Int]  
  
pop :: Stack -> (Int,Stack)  
pop (x:xs) = (x,xs)  
  
push :: Int -> Stack -> ((),Stack)  
push a xs = ((),a:xs)

stackManip = do  
    push 3  
    a <- pop  
    pop
newtype State s a = State { runState :: s -> (a,s) }

instance Monad (State s) where  
    return x = State $ \\s -> (x,s)  
    (State h) >>= f = State $ \\s -> let (a, newState) = h s  
                                        (State g) = f a  
                                    in  g newState
import Control.Monad.State  
  
pop :: State Stack Int  
pop = State $ \\(x:xs) -> (x,xs)  
  
push :: Int -> State Stack ()  
push a = State $ \\xs -> ((),a:xs)

stackManip :: State Stack Int  
stackManip = do  
    push 3  
    a <- pop  
    pop

Random