import Termcap import IO import Monad data State a b = State ( a -> ( b , a ) ) instance Monad (State a) where return b = State ( \a -> ( b , a ) ) (State st) >>= f = State ( \a -> let ( b , a' ) = st a (State trans) = f b in trans a' ) retrieve :: ( a -> b ) -> State a b retrieve f = State ( \a -> ( f a , a ) ) update :: ( a -> a ) -> State a () update f = State ( \a -> ( () , f a ) ) input :: IO ( State a Char ) input = do c <- getChar return ( State ( \a -> ( c , a ) ) ) -- This function sends the current state to standard out -- Somehow the state upto this point is calculated and -- fed to STDOUT. Can't work out how to express this -- in Haskell output :: ( a -> String ) -> IO ( State a () ) run :: State a b -> a -> b run (State trans) init = (fst.trans) init stateAction :: State a b -> IO ( State a b ) stateAction act = return ( do act ) extr = stateAction.retrieve upd = stateAction.update finalise :: State a b -> a -> b finalise (State trans) a = trans a