【发布时间】:2012-05-29 08:59:25
【问题描述】:
我的功能如下:
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 中的某个类型)?
感谢您的帮助!
【问题讨论】:
标签: haskell types type-inference