Reputation: 159
I am a newbie to haskell and have a working code below as follows.
sum' :: (Num a) => [a] -> a
sum' [] = 0
sum' (x:xs) = x + sum' xs
What i try to do is to be more specific about my types.Instead of using 'a' only i try to write my code like that.
sum' :: (Num a, Num b) => [a] -> b
sum' [] = 0
sum' (x:xs) = x + sum' xs
When i do is i get an error.Error is as follows: Couldn't match expected type ‘b’ with actual type ‘a’ ‘a’ is a rigid type variable bound by the type signature for sum' :: (Num a, Num b) => [a] -> b at baby.hs:2:9 ‘b’ is a rigid type variable bound by the type signature for sum' :: (Num a, Num b) => [a] -> b at baby.hs:2:9
I think i don't really understand the meaning of '=>'. The part confuses me is what comes before '=>'. Sometimes a thing like that '(Show a) =>' and usually just types just like in my code. Please help thanks in advance.
Upvotes: 1
Views: 147
Reputation: 119847
Instead of using 'a' only i try to write my code like that.
This doesn't work because +
requires two arguments of the same type, and returns a result of that very type.
:t (+)
(+) :: Num a => a -> a -> a
Since there's no way to add two Integer
s and get back a Double
, or add an Integer
and a Double
, there is also no way to sum a list of Integer
s and get back a Double
.
Num a =>
means that the type a
must be a number. More precisely, it requires that there is an instance Num a
of the type class Num
exists.
More info about type classes can be found here
Upvotes: 3
Reputation: 435
In the first code snippet, you're saying "Take a list of a
s (that are an instance of typeclass Num), and return something that's the same type as the elements in that list (that type is represented by a
in the entire type signature)." That's what you want.
The second snippet says "Take a list of a
s (that are an instance of typeclass Num), and return something of type b
, a different type." Since (+) returns the same type as its parameters, your function must as well.
The Num a
in your first snippet applies to all a
s in the type signature, so there's no reason to split them into a
and b
.
Upvotes: 0