------------------------------------------------------------------------------
-- Ex 7: here is the Result type from Set12. Implement a Monad Result
-- instance that behaves roughly like the Monad Maybe instance.
--
-- That is,
-- 1. MkResult behave like Just
-- 2. If part of computation produces NoResult, the whole computation
-- produces NoResult (just like Nothing)
-- 3. Similarly, if we get a Failure "reason" value, the whole
-- computation produces Failure "reason"
--
-- Examples:
-- MkResult 1 >> Failure "boom" >> MkResult 2
-- ==> Failure "boom"
-- MkResult 1 >> NoResult >> Failure "not reached"
-- ==> NoResult
-- MkResult 1 >>= (\x -> MkResult (x+1))
-- ==> MkResult 2
data Result a = MkResult a | NoResult | Failure String deriving (Show,Eq)
instance Functor Result where
-- The same Functor instance you used in Set12 works here.
fmap = todo
-- This is an Applicative instance that works for any monad, you
-- can just ignore it for now. We'll get back to Applicative later.
instance Applicative Result where
pure = return
(<*>) = ap
instance Monad Result where
-- implement return and >>=
return = todo
(>>=) = todo
I am able to clear all tests by only defining the Monad instance functions, while leaving the Functor instance fmap definition as todo. Even though the definition can be copied from Set 12 (as stated in the exercise) or just defined as fmap = liftM, I would guess the intention is to not let students leave todos in the code.
The Exercise 7 of Set 13b is as follows:
I am able to clear all tests by only defining the
Monad
instance functions, while leaving theFunctor
instancefmap
definition astodo
. Even though the definition can be copied from Set 12 (as stated in the exercise) or just defined asfmap = liftM
, I would guess the intention is to not let students leavetodo
s in the code.