I have a simple function defined below:
allZero :: Num a => [a]-> Bool
allZero [] = False
allZero xs = and (map (== 0) xs)
This returns an error message upon loading:
Could not deduce (Num a) arising from the literal `0'
What is the problem with this function? How do I oveload the number 0 to be of any numeric type a?
This code compiles once you add an Eq a
constraint to allZero
. There should be no other issues compiling this code.
allZero :: (Num a, Eq a) => [a]-> Bool
allZero [] = False
allZero xs = and (map (== 0) xs)
As some commenters pointed out, the first case is non-standard, as allZero
of []
is typically True
. With that definition, the first case becomes redundant.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.