[n] -> [n] -> n -> n -- heist [] [] _ = 0 heist w v maxw = func w v i j where -6ren">
gpt4 book ai didi

haskell - 为什么这会给我 "is a rigid type variable bound by"错误

转载 作者:行者123 更新时间:2023-12-02 12:21:38 26 4
gpt4 key购买 nike

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 类型系统

最佳答案

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

关于haskell - 为什么这会给我 "is a rigid type variable bound by"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7979413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com