为什么这给了我“是一个严格的类型变量绑定”错误

时间:2011-11-02 11:14:43

标签: 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

上面的代码给了我:

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型系统一寸一寸地把头包住了

2 个答案:

答案 0 :(得分:6)

length始终返回Int。将i传递给func,您说n应该是Int,但heist希望n是通用的,因此类型错误。

答案 1 :(得分:5)

length返回Int;使用i = Data.List.genericLength wi = fromIntegral (length w)