Haskell 为什么这给了我;是一个刚性类型变量,由;错误
上面的代码给了我:Haskell 为什么这给了我;是一个刚性类型变量,由;错误,haskell,types,Haskell,Types,上面的代码给了我: 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 抢劫案。hs:15:27: 无法推断(
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
抢劫案。hs:15:27:
无法推断(n~Int)
从上下文(Num n)
由的类型签名绑定
抢劫::数量n=>[n]->[n]->n->n
在抢劫案中:(15,1)-(17,16)
`n'是一个刚性类型变量,由
heist的类型签名::Num n=>[n]->[n]->n->n
在抢劫案中,hs:15:1
在'func'的第三个参数中,即'i'
在表达式中:func w v i j
在“抢劫”的方程式中:
抢劫案
=函数w v i j
哪里
i=长度w
j=maxw
为什么会这样
我的头一寸一寸地绕着Haskell类型的系统
length
返回一个Int
;使用i=Data.List.genericLength w
或i=from integral(length w)
length
始终返回Int
。通过将i
传递给func
你是说n
应该是Int
,但是heist
希望n
是泛型的,因此类型错误。hmm,请详细说明?我想Int
是Num
的一个例子,为什么它还需要那种“fromIntegral”转换呢?heist
的类型承诺“你给我一个任意类型n
和numn
,我给你一个[n]->[n]->n->n
类型的函数。”。因此,假设我们得到某个类型n
(“刚性类型变量”),对此我们一无所知(除了它是Num
的一个实例)。然后在n=Int
处调用func
,它再次返回Int
。但我们应该返回给定的n
类型的值。简言之,您的类型签名承诺heist
也将对Double(以及其他)有效。但事实并非如此。
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