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.
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:
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. Thanks.
I think the problem is you're not actually apply allDivisors
to anything:
AS.sumAllSubsets (DFL.allDivisors)
is just applying sumAllSubsets
to the function allDivisors
, not to its [Integer]
return value. Maybe you meant AS.sumAllSubsets (DFL.allDivisors n)
, that is, applying allDivisors
to n
?
(BTW, findWeird
is just doing a filter
, and can be written as
findWeird nn = filter (\n -> n `notElem` (sumAllSubsets $ allDivisors n)) nn
where I've also taken the liberty to reduce some nesting via the ($)
operator .)
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.