I am currently trying to do 20 Intermediate Haskell Exercises . I managed to complete the 1st 3 exercises (but this is due to the fact that furry == fmap and Learn You the Haskell already have these implementations). I am now stuck in an instance that says:
instance Fluffy (EitherLeft t) where furry = error "todo"
I don’t quite understand what to do. In Learn You Haskell, they have a newtype variable called Pair , which takes a tuple. Then they can perform pattern matching as such:
fmap f (Pair (x,y)) = Pair (fx, y)
I was thinking maybe you could do something like this in my situation:
furry f (EitherLeft (Either ab)) = EitherLeft (Either (fa) b)
But this does not work:
Not in scope: data constructor `Either'
I thought maybe I would import Data.Either , because there may be some things that I don't have. But that didn't matter.
I also tried to get this to work:
furry f (EitherLeft ab) = error "todo"
But this also does not work:
Constructor `EitherLeft' should have 1 argument, but has been given 2
I could not get this to work:
furry f (Right x) = (Right fx) furry f (Left x) = Left x
Which gave an error:
Couldn't match expected type `EitherLeft ta' with actual type `Either t0 t1'
I managed to get:
furry f (EitherLeft t) = error "todo"
work. But I have no idea what to do with t .
I don’t necessarily need an answer. I just need a hint on what I should do, because I am reading, and I can somehow understand the examples, but I can’t figure out how to code this correctly.
Thanks, Dan, here is what I came up with for my solution:
instance Fluffy (EitherLeft t) where furry f (EitherLeft (Left x)) = EitherLeft $ Left (fx) furry f (EitherLeft (Right x)) = EitherLeft $ Right x