Haskell 不能';t匹配类型'Integer';带有'Int';
我正在进行Euler#41项目,这是我解决问题的一部分,我不知道如何绕过Haskell,希望使用Haskell 不能';t匹配类型'Integer';带有'Int';,haskell,Haskell,我正在进行Euler#41项目,这是我解决问题的一部分,我不知道如何绕过Haskell,希望使用[Int]而不是[Integer]。Diglist工作正常,但我将其包括在内,以防错误涉及到它 digList :: Integer -> [Integer] digList n = digList' [] n where digList' xs n | n < 10 = n : xs | oth
[Int]
而不是[Integer]
。Diglist工作正常,但我将其包括在内,以防错误涉及到它
digList :: Integer -> [Integer]
digList n = digList' [] n where
digList' xs n
| n < 10 = n : xs
| otherwise = digList' (lsd : xs) nxt
where
lsd = n `mod` 10
nxt = n `div` 10
isNPandigital :: Integer -> Bool
isNPandigital n = isnp 1 (digList n) where --error on this line
isnp i xs
| i `elem` xs = isnp i $ delete i xs
| i == length (digList n) = null xs
| otherwise = False
由于您使用
i`elem`xs
,并且由于您使用i==length(digList n)
,因此将i
到isnp
的参数推断为Int
。编译器看到i
应该是Int
,因为您将它与length(digList n)
进行比较,后者总是返回Int
,并且您使用的是i`elem`xs
,因此xs
必须具有[Int]
,但是您为xs
传递(digList n)
,而diglistn
的类型为[Integer]
,因此出现了错误。很有趣。在Haskell中处理这种类型差异的惯用方法是什么?最后我做了(toInteger.length.digList)xs
,这样ghc就可以推断I
的类型为Integer
,但是对toInteger的额外调用似乎。。。对我来说太脏了。@wolf你也可以使用genericLength::Num i=>[a]->i
。它略慢于长度
,但可以返回任何类型的数字。
Couldn't match type `Integer' with `Int'
Expected type: [Int]
Actual type: [Integer]
In the return type of a call of `digList'
In the second argument of `isnp', namely `(digList n)'
In the expression: isnp 1 (digList n)