when I try to compile this piece of code
prod [] = 1
prod (x:xs) = x * prod xs
ff :: (Num a) => a -> a -> a
ff x n = prod [(x - n + 1) .. x]
I get following error:
a.hs:5:15:
Could not deduce (Enum a)
arising from the arithmetic sequence `(x - n + 1) .. x'
from the context (Num a)
bound by the type signature for ff :: Num a => a -> a -> a
at a.hs:5:1-32
Possible fix:
add (Enum a) to the context of
the type signature for ff :: Num a => a -> a -> a
In the first argument of `prod', namely `[(x - n + 1) .. x]'
In the expression: prod [(x - n + 1) .. x]
In an equation for `ff': ff x n = prod [(x - n + 1) .. x]
what is wrong with this code? When I substitute Double for a everything is all right.
[i .. j]is shorthand forenumFromTo i j.enumFromTois part of theEnumtypeclass, and not part ofNum(you still needNumto use+and-though).So you need to say that
aimplementsEnumas well as implementingNum:It works with
DoublebecauseDoubleimplements both these typeclasses.