Haskell 99 题 #7:添加到列表前与添加到列表中

Haskell 99 Problems #7: Prepend vs Append to List

这通过了:

data Nested List a = Elem a | List [NestedList a]
flatten :: NestedList a -> [a]
flatten (Elem a) = [a]
flatten (List (x:xs)) = flatten x ++ flatten (List xs)
flatten (List []) = []

这失败了:

data NestedList a = Elem a | List [NestedList a]
flatten :: NestedList a -> [a]
flatten (Elem a) = [a]
flatten (List (x:xs)) = flatten x : flatten (List xs)
flatten (List []) = []

错误是:

Couldn't match expected type `a' with actual type `[a]'
  `a' is a rigid type variable bound by
      the type signature for flatten :: NestedList a -> [a]
      at 007.hs:2:12
Relevant bindings include
  xs :: [NestedList a] (bound at 007.hs:4:18)
  x :: NestedList a (bound at 007.hs:4:16)
  flatten :: NestedList a -> [a] (bound at 007.hs:3:1)
In the first argument of `(:)', namely `flatten x'
In the expression: flatten x : flatten (List xs)

不同之处在于 ++ 而不是 :。我知道前者附加,后者附加,但为什么 : 在这种情况下不起作用?我不明白错误。

实际上 ++ 是串联运算符,在这种特殊情况下,它希望这两个运算符都是一个列表。由于 flatten returns 一个列表,++ 愉快地连接它们。

但是,: 只是将一个项目添加到列表中。由于 flatten xflatten x : flatten (List xs) returns 列表中,: 抛出此错误。