无法将预期类型[a]与实际类型`Integer - >匹配[整数]'

时间:2014-06-30 12:07:19

标签: haskell

我有以下代码,它们找到一个整数的除数,然后是除数的所有子集,然后对每个子集中的所有除数求和,然后测试是否在求和列表中表示该整数。在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))

但据我所知,所有的Divisors都会产生[Integral]而sumAllSubsets会产生[Integral],所以我只是想知道是否有人可以提供帮助。感谢。

1 个答案:

答案 0 :(得分:3)

我认为问题是你实际上并没有将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

我已经冒昧地通过($) operator减少了一些嵌套。)