我无法将以下程序加载到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)
Run Code Online (Sandbox Code Playgroud)
当我编译它时,我收到以下错误消息:
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
Run Code Online (Sandbox Code Playgroud)
max x y将返回一个value(a),而不是list([a]).你只能将cons(:)放入一个列表中.你需要写,而不是:
(min x y : [max x y])
Run Code Online (Sandbox Code Playgroud)