Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 12th, 2012  |  syntax: Haskell  |  size: 0.81 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. data Logger a = Logger ([String]->([String], a))
  3.  
  4. instance Monad Logger where
  5.   return a = Logger (\x->(x, a))
  6.   (Logger a) >>= b = Logger (loggerBind a b)
  7.  
  8. loggerBind a b prevLog =
  9.   let (newLog, aValue)     = a prevLog
  10.       (Logger newLogger)   = b aValue
  11.       (newLog', newValue') = newLogger newLog
  12.   in (newLog', newValue')
  13.  
  14. putLog str = Logger (\x->(str:x, ()))
  15.  
  16. loggedAdd x y = do
  17.   putLog ("Adding " ++ (show x) ++ " and " ++ (show y) ++ "!")
  18.   return (x + y)
  19.  
  20. loggedSubtract x y = do
  21.   putLog ("Subtracting " ++ (show y) ++ " from " ++ (show x) ++ "!")
  22.   return (x - y)
  23.  
  24. loggedMul x y = do
  25.   putLog ("Multiplying " ++ (show x) ++ " and " ++ (show y) ++ "!")
  26.   return (x * y)
  27.  
  28. calculate a b = do
  29.   s <- loggedAdd a b
  30.   sqr <- loggedMul s s
  31.   return sqr
  32.  
  33. runLogger (Logger a) = a []