2
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
这是为什么发生?
我被英寸
嗯,请详述一下吗?我认为'Int'是'Num'的一个实例,为什么它仍然需要这种“fromIntegral”转换? – nobody
'heist'的类型承诺“你给我一个任意类型'n'和'Num n',我会给你一个类型为[n] - > [n] - > n - > n'的函数” 。所以假设我们给了一些'n'(“刚性类型变量”),关于它我们什么也不知道(除了它是'Num'的一个实例)。然后我们在'n = Int'处调用'func',它又返回一个'Int'。但是我们应该返回给定的'n'类型的值。 – FunctorSalad
简而言之,您的类型签名承诺'heist'也适用于Double(以及其他)。但事实并非如此。 – Ingo