I am learning Haskell and having trouble returning literal 1 as Maybe Integer
alwaysOne:: Integer -> Maybe Integer
alwaysOne n = 1
raises
error:
• No instance for (Num (Maybe Integer))
arising from the literal ‘1’
• In the expression: 1
In an equation for ‘alwaysOne’: alwaysOne n = 1
|
14 | alwaysOne n = 1
| ^
>Solution :
having trouble returning literal
1asMaybe Integer
The Maybe a type has two data constructors:
data Maybe a = Nothing | Just a
You need to use one of these data constructors if you want to produce a Maybe Integer value. What you want is to pass the 1 to the Just data constructor:
alwaysOne:: Integer -> Maybe Integer
alwaysOne n = Just 1
That is, Just takes a value of any type and produces a value of Maybe parametrized for that type:
Just :: a -> Maybe a
a above is inferred as Integer in your case.
Analogously, if you wanted to define alwaysNothing:
alwaysNothing :: Integer -> Maybe Integer
alwaysNothing _ = Nothing