简体   繁体   English

无法将期望的类型[a]与实际类型“整数-> [整数]”匹配

[英]Couldn't match expected type [a] with actual type `Integer -> [Integer]'

I have the following code that finds the divisors of an Integer, then all the subsets of the divisors, then sums all the divisors in each subset, then tests to see if that Integer is represented in the list of summations. 我有以下代码,先找到一个整数的除数,然后找到除数的所有子集,然后求和每个子集中的所有除数,然后进行测试以查看该整数是否在求和列表中表示。 In findWeird this is performed over a list of Integers. 在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

Problem is that I get the error: 问题是我得到了错误:

test.hs:15:61: 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)) 

But as far as know allDivisors produces a [Integral] and sumAllSubsets takes an [Integral], so I was just wondering if anyone could help. 但是就目前所知,allDivisors产生一个[Integral],sumAllSubsets产生一个[Integral],所以我只是想知道是否有人可以提供帮助。 Thanks. 谢谢。

I think the problem is you're not actually apply allDivisors to anything: 我认为问题是您实际上并未将allDivisors应用于任何事物:

AS.sumAllSubsets (DFL.allDivisors)

is just applying sumAllSubsets to the function allDivisors , not to its [Integer] return value. 只是将sumAllSubsets应用于函数 allDivisors ,而不是其[Integer]返回值。 Maybe you meant AS.sumAllSubsets (DFL.allDivisors n) , that is, applying allDivisors to n ? 也许您是说AS.sumAllSubsets (DFL.allDivisors n) ,也就是说,将allDivisors应用于n


(BTW, findWeird is just doing a filter , and can be written as (顺便说一句, findWeird只是做一个filter ,可以写成

findWeird nn = filter (\n -> n `notElem` (sumAllSubsets $ allDivisors n)) nn

where I've also taken the liberty to reduce some nesting via the ($) operator .) 在这里我还通过($)运算符自由地减少了一些嵌套。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Haskell无法将期望的类型“ Integer”与实际类型“ [Integer]”进行匹配 - Haskell Couldn't match expected type `Integer' with actual type `[Integer]' 无法将预期类型“ [Integer]”与实际类型“ Integer”匹配 - Couldn't match expected type ‘[Integer]’ with actual type ‘Integer’ 无法将预期的类型&#39;Integer-&gt; t&#39;与实际类型匹配 - Couldn't match expected type 'Integer -> t' with actual type 无法将预期类型&#39;Integer - &gt; t&#39;与实际类型&#39;Bool&#39;匹配 - Couldn't match expected type ‘Integer -> t’ with actual type ‘Bool’ 无法将预期类型`a`与实际类型`Integer`相匹配 - Couldn't match expected type `a` with actual type `Integer` 无法将预期类型“Bool”与实际类型“[[Integer]] 匹配 - Couldn't match expected type `Bool' with actual type `[[Integer]] 无法将预期类型“Int”与实际类型“Integer”匹配 - Couldn't match expected type `Int' with actual type `Integer' 无法将预期类型“[Integer]”与实际类型“Bool”匹配 - Couldn't match expected type `[Integer]' with actual type `Bool' 无法将预期类型&#39;Integer&#39;与实际类型&#39;m0 Integer&#39;匹配 - Couldn't match expected type ‘Integer’ with actual type ‘m0 Integer’ Fibonacci secuence 无法将预期类型 &#39;a0 -&gt; t&#39; 与实际类型 &#39;[Integer]&#39; 匹配 - Fibonacci secuence Couldn't match expected type ‘a0 -> t’ with actual type ‘[Integer]’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM