Skip to content

Commit

Permalink
Error Monad.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabi Volpe committed Aug 23, 2016
1 parent 50a8a84 commit 0cc9dbd
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions category-theory/error-monad.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Control.Monad.Error

-- definition of the error monad on top of Either
-- instance (Error e) => Monad (Either e) where
-- return x = Right x
-- Right x >>= f = f x
-- Left err >>= f = Left err
-- fail msg = Left (strMsg msg) -- the strMsg function is defined in the Error class

a1 = Left "boom" >>= \x -> return (x+1)
a2 = Right 100 >>= \x -> Left "no way!"

-- very important to define the type at the end!!!
b1 = Right 3 >>= \x -> return (x + 100) :: Either String Int

0 comments on commit 0cc9dbd

Please sign in to comment.