我无法将以下程序加载到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
答案 0 :(得分:8)
max x y
将返回值(a
),而不是列表([a]
)。您只能将(:
)纳入列表。你将需要写作:
(min x y : [max x y])