我已经为此苦苦挣扎了半个多小时。我知道这很简单,但是我在 Haskell 中的类型很糟糕,即使在阅读了与我的问题非常相似的公认答案之后,我仍然无法解决我的问题 - 更不用说理解它了!
编码:
p108 = [filter (\[a,b] -> a>0 && b>0) (diophantinepairs n) | n <- [1..]]
diophantinepairs :: Integer -> [[Integer]]
diophantinepairs n = nub$map sort b
where
a = divisors n
b = [[(n-d), n - (n^2)/d] | d <- a]
错误 :
249:39: No instance for (Fractional Integer) arising from a use of `/' Possible fix: add an instance declaration for (Fractional Integer) In the second argument of `(-)', namely `(n ^ 2) / d' In the expression: n - (n ^ 2) / d In the expression: [(n - d), n - (n ^ 2) / d]
谢谢,山姆。