有什么区别++和:在haskell中?

Mic*_*cah 4 haskell ghci

我不明白 -

Prelude> "hi"++"there"
"hithere"
Prelude> "hi":"there"

<interactive>:12:6:
    Couldn't match expected type `[Char]' with actual type `Char'
    Expected type: [[Char]]
      Actual type: [Char]
    In the second argument of `(:)', namely `"there"'
    In the expression: "hi" : "there"
Prelude> 
Run Code Online (Sandbox Code Playgroud)

为什么不回归"hithere"?

Lil*_*ard 12

类型.在GCHi中试试这个:

Prelude> :t (:)
(:) :: a -> [a] -> [a]
Prelude. :t (++)
(++) :: [a] -> [a] -> [a]
Run Code Online (Sandbox Code Playgroud)


Mic*_*cah 6

我现在明白了.第一个运算符需要是元素,而不是列表.

所以如果我这样'h':"ithere"做会回来"hithere"

  • 在我看来,这篇文章可以被认为是一个答案. (8认同)