I can multiply a list per 2:
(* 2) <$> [1, 2, 3]
But I want multiply the elements which are Just:
(* 2) <$> [Just 1, Nothing, Just 3]
Error:
* Non type-variable argument in the constraint: Num (Maybe a) (Use FlexibleContexts to permit this) * When checking the inferred type it :: forall a. (Num (Maybe a), Num a) => [Maybe a] Prelude Data.List
Another try:
fmap (* 2) [Just 1, Nothing, Just 3]
Error:
* Non type-variable argument in the constraint: Num (Maybe a) (Use FlexibleContexts to permit this) * When checking the inferred type it :: forall a. (Num (Maybe a), Num a) => [Maybe a]
I have tried more things: map2, fmap2, map (*2) map, etc.
You need to map twice: once to get inside the list, second time to get inside the maybe. The operator <$>
only maps once, and you can't use the same operator twice, so you have to add a call to fmap
:
fmap (* 2) <$> [Just 1, Nothing, Just 3]