
Untitled
By: a guest on
Apr 12th, 2012 | syntax:
Haskell | size: 0.81 KB | hits: 27 | expires: Never
data Logger a = Logger ([String]->([String], a))
instance Monad Logger where
return a = Logger (\x->(x, a))
(Logger a) >>= b = Logger (loggerBind a b)
loggerBind a b prevLog =
let (newLog, aValue) = a prevLog
(Logger newLogger) = b aValue
(newLog', newValue') = newLogger newLog
in (newLog', newValue')
putLog str = Logger (\x->(str:x, ()))
loggedAdd x y = do
putLog ("Adding " ++ (show x) ++ " and " ++ (show y) ++ "!")
return (x + y)
loggedSubtract x y = do
putLog ("Subtracting " ++ (show y) ++ " from " ++ (show x) ++ "!")
return (x - y)
loggedMul x y = do
putLog ("Multiplying " ++ (show x) ++ " and " ++ (show y) ++ "!")
return (x * y)
calculate a b = do
s <- loggedAdd a b
sqr <- loggedMul s s
return sqr
runLogger (Logger a) = a []