Reputation: 2967
The below code won't compile:
data Outcome a = Fail | Pass a deriving (Show, Eq, Ord, Functor)
myList = [Pass 33, Pass 12, Fail, Pass 45]
main = do
print $ fmap (+1) myList
I can't see why this doesn't work, as I've successfully applied the same approach to my binary tree of type 'data Tree a = Empty | Node a (Tree a) (Tree a)'?
Upvotes: 1
Views: 169
Reputation: 23870
The problem is that you are trying to add 1 to each element of the list myList
which has type Outcome Integer
. Try this instead:
fmap (fmap (+1)) myList
or to make it more clear:
map (fmap (+1)) myList
So the outer fmap
applies to the list and the inner one applies to the Outcome
functor. So, to each element of the list, we apply the function fmap (+1)
, which in turn applies the function (+1)
to the value inside the Outcome
.
Upvotes: 11
Reputation: 144136
Lists are functors and you have an [Outcome]
so you need to supply a function Outcome -> b
. It looks like you want to apply (+1)
to each Outcome
within the list so you need to use:
fmap (\o -> fmap (+1) o) myList
or
fmap (fmap (+1)) myList
Upvotes: 6