All about monads, exercise 4

Hey, I am currently reading "All about monads". Unfortanlty I have only a pdf without the solutions to the exercises. in the previous exercises I defined: data Sheep = Sheep (Maybe Sheep) (Maybe Sheep) father :: Sheep -> Maybe Sheep father (Sheep f m) = f mother :: Sheep -> Maybe Sheep mother (Sheep f m) = m parent :: Sheep -> Maybe Sheep parent s = (mother s) `mplus` (father s) parentList :: Sheep -> [Sheep] parentList s = (maybeToList (mother s)) `mplus` (maybeToList (father s)) Now I am supposed to combine "parent" and "parentList" to a function parent :: MonadPlus m => Sheep -> m Sheep Problem, my parent function relies on "mother" and "father", and they give a Maybe monad. So I need: mother :: MonadPlus m => Sheep -> m Sheep do I not? But I have no Idea of how the body of this function would look. Anyone can give me a hint? Thanks! Nathan but what I really need, is a function

I have a solution, so for anyone interested I now do: father :: MonadPlus m => Sheep -> m Sheep father (Sheep Nothing _) = mzero father (Sheep (Just f) _) = return f mother :: MonadPlus m => Sheep -> m Sheep mother (Sheep _ Nothing) = mzero mother (Sheep _ (Just m)) = return m On 05/01/2011 01:14 AM, Nathan Hüsken wrote:
Hey,
I am currently reading "All about monads". Unfortanlty I have only a pdf without the solutions to the exercises.
in the previous exercises I defined:
data Sheep = Sheep (Maybe Sheep) (Maybe Sheep)
father :: Sheep -> Maybe Sheep father (Sheep f m) = f
mother :: Sheep -> Maybe Sheep mother (Sheep f m) = m
parent :: Sheep -> Maybe Sheep parent s = (mother s) `mplus` (father s)
parentList :: Sheep -> [Sheep] parentList s = (maybeToList (mother s)) `mplus` (maybeToList (father s))
Now I am supposed to combine "parent" and "parentList" to a function parent :: MonadPlus m => Sheep -> m Sheep
Problem, my parent function relies on "mother" and "father", and they give a Maybe monad. So I need: mother :: MonadPlus m => Sheep -> m Sheep
do I not? But I have no Idea of how the body of this function would look.
Anyone can give me a hint? Thanks! Nathan
but what I really need, is a function
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (1)
-
Nathan Hüsken