如何解决此类错误?

时间:2011-10-21 21:18:10

标签: haskell

我无法将以下程序加载到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

1 个答案:

答案 0 :(得分:8)

max x y将返回值(a),而不是列表([a])。您只能将(:)纳入列表。你将需要写作:

(min x y : [max x y]) 
相关问题