Problems understanding a system like Haskell

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 
+8
constructor types overloading algebraic-data-types haskell
source share
1 answer

The problem you are facing is that any data type does not have a data constructor called “Either”, basically the “Either” type looks like this:

 data Either ab = Left a | Right b 

Thus, the value may be of type Either ab , but there is no such value as Either "one" 1 or something similar, but instead Left "one" or Right 1 .

So, in the case of EitherLeft , in a similar way, its values ​​will look like EitherLeft (Left a) or EitherLeft (Right b) and should match the pattern as such.

+13
source share

All Articles