如何解决此类型错误?
我无法将以下程序加载到 GHCi:
minList :: Ord a => [a] -> a
minList (x:[]) = x
minList (x:y:xs) = minList( min x y : xs)
bubList :: Ord a => [a] -> [a]
bubList [] = []
bubList ( x:y:[] ) = min x y : max x y
bubList ( x:y:xs ) = minList(x:y:xs) : bubList(xs)
当我编译它时,我收到以下错误消息:
Couldn't match type `a' with `[a]'
`a' is a rigid type variable bound by
the type signature for bubList :: Ord a => [a] -> [a]
at ex1.hs:11:1
In the second argument of `max', namely `y'
In the second argument of `(:)', namely `max x y'
In the expression: min x y : max x y
I fail to load the following program to GHCi:
minList :: Ord a => [a] -> a
minList (x:[]) = x
minList (x:y:xs) = minList( min x y : xs)
bubList :: Ord a => [a] -> [a]
bubList [] = []
bubList ( x:y:[] ) = min x y : max x y
bubList ( x:y:xs ) = minList(x:y:xs) : bubList(xs)
When I compile it I get the following error message:
Couldn't match type `a' with `[a]'
`a' is a rigid type variable bound by
the type signature for bubList :: Ord a => [a] -> [a]
at ex1.hs:11:1
In the second argument of `max', namely `y'
In the second argument of `(:)', namely `max x y'
In the expression: min x y : max x y
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
max x y
将返回一个值 (a
),而不是列表 ([a]
)。您只能将 cons (:
) 放入列表中。你需要写:max x y
will return a value (a
), not a list ([a]
). You can only cons (:
) into a list. You are going to need to write, instead: