I'm a beginner of Haskell. Now I tried to define a data as following:
data Unsure a = Sure a |Error [Char]
deriving (Show)
then tried to implement Functor like this:
instance Functor Unsure where
fmap f (Sure x) = Sure (f x)
fmap f (Error e) = Error e
In my opinion, fmap should work Once I implemented Functor for Unsure. So I can use fmap to do something like:
fmap (+3) (+100) Sure 1
The result was supposed to be Sure 104 , in fact I got a error
• Non type-variable argument in the constraint: Num (Unsure a) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall a. (Num (Unsure a), Num a) => Unsure a
Any suggestion?
fmap (+3) (Sure 1)
things would have worked, as would havefmap (+3) (+100) 1
(for subtle reasons). If you had writtenfmap (+3) Sure 1
you might have gotten a reasonable error message, butfmap (+3) (+100) Sure 1
is a perfect storm that creates a nasty error message. – cody100
inside the parentheses:fmap (==100) (Sure 100)
<-- returnsSure True
. – Fyodor Soikin