![](/img/trans.png)
[英]Haskell Couldn't match expected type `Integer' with actual type `[Integer]'
[英]Couldn't match expected type [a] with actual type `Integer -> [Integer]'
我有以下代码,先找到一个整数的除数,然后找到除数的所有子集,然后求和每个子集中的所有除数,然后进行测试以查看该整数是否在求和列表中表示。 在findWeird中,这是对整数列表执行的。
allSubsets :: (Integral a ) => [a] -> [[a]]
allSubsets [] = [[]]
allSubsets ( n : nr ) = allSubsets nr ++ map (n:) (allSubsets nr)
sumAllSubsets :: (Integral a ) => [a] -> [a]
sumAllSubsets s = map sum (allSubsets s)
allDivisors :: (Integral a) => a -> [a]
allDivisors 0 = []
allDivisors n | n > 0 = [d | d <- [1..n], n `mod` d == 0]
| n < 0 = -1 : allDivisors (-n)
findWeird :: (Integral a) => [a] -> [a]
findWeird [] = []
findWeird (n:nn) = ( if n `elem` (AS.sumAllSubsets (DFL.allDivisors))
then []
else [n]) ++ findWeird nn
问题是我得到了错误:
test.hs:15:61:
Couldn't match expected type `[a]' with actual type `Integer -> [Integer]' In the first argument of `sumAllSubsets', namely `(allDivisors)' In the second argument of `elem', namely `(sumAllSubsets (allDivisors))' In the expression: n `elem` (sumAllSubsets (allDivisors))
但是就目前所知,allDivisors产生一个[Integral],sumAllSubsets产生一个[Integral],所以我只是想知道是否有人可以提供帮助。 谢谢。
我认为问题是您实际上并未将allDivisors
应用于任何事物:
AS.sumAllSubsets (DFL.allDivisors)
只是将sumAllSubsets
应用于函数 allDivisors
,而不是其[Integer]
返回值。 也许您是说AS.sumAllSubsets (DFL.allDivisors n)
,也就是说,将allDivisors
应用于n
?
(顺便说一句, findWeird
只是做一个filter
,可以写成
findWeird nn = filter (\n -> n `notElem` (sumAllSubsets $ allDivisors n)) nn
在这里我还通过($)
运算符自由地减少了一些嵌套。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.