我的功能如下:
foo :: Int -> a -> [a]
foo n v = bar n
where
bar :: Int -> [a]
bar n = take n $ repeat v
使用 ghci 报此错误:
Couldn't match type `a' with `a1'
`a' is a rigid type variable bound by
the type signature for foo :: Int -> a -> [a] at hs99.hs:872:1
`a1' is a rigid type variable bound by
the type signature for bar :: Int -> [a1] at hs99.hs:875:9
Expected type: [a1]
Actual type: [a]
In the expression: take n $ repeat v
In an equation for `bar': bar n = take n $ repeat v
如果去掉 bar 的类型声明,代码可以正确编译。那么 bar 的正确类型声明是什么?为什么会发生错误,因为 bar 的类型声明比 bar 的定义更通用(绑定到 foo 中的某些类型)?
谢谢你的帮助!