heist :: (Num n) => [n] -> [n] -> n -> n
-- heist [] [] _ = 0
heist w v maxw = func w v i j where
i = length w
j = maxw
func :: (Num n) => [n] -> [n] -> n -> n -> n
func _ _ 0 0 = 0
上面的代码给了我:
Heist.hs:15:27: Could not deduce (n ~ Int) from the context (Num n) bound by the type signature for heist :: Num n => [n] -> [n] -> n -> n at Heist.hs:(15,1)-(17,16) `n' is a rigid type variable bound by the type signature for heist :: Num n => [n] -> [n] -> n -> n at Heist.hs:15:1 In the third argument of `func', namely `i' In the expression: func w v i j In an equation for `heist': heist w v maxw = func w v i j where i = length w j = maxw
为什么会这样?
我围着Haskell型系统一寸一寸地把头包住了
答案 0 :(得分:6)
length
始终返回Int
。将i
传递给func
,您说n
应该是Int
,但heist
希望n
是通用的,因此类型错误。
答案 1 :(得分:5)
length
返回Int
;使用i = Data.List.genericLength w
或i = fromIntegral (length w)
。