Learn you a Haskell for great good と 40 ページの as-patterns を読んでいます。
例を次のように少し変更しました。
firstLetter :: String -> String
firstLetter "" = "Empty string, oops"
firstLetter all@(x:xs) = "The first letter of " ++ all ++ " is " ++ [x] ++ " otherbit " ++ xs
次に、次のように使用できます。
*Main> firstLetter "Qwerty"
"The first letter of Qwerty is Q otherbit werty"
しかし、[x] と x の違いと、上記の例で [x] を使用しなければならない理由について混乱しました。
たとえば、私がに変更した場合
firstLetter :: String -> String
firstLetter "" = "Empty string, oops"
firstLetter all@(x:xs) = "The first letter of " ++ all ++ " is " ++ x ++ " otherbit " ++ xs
エラーが発生します:
Couldn't match expected type `[Char]' with actual type `Char'
In the first argument of `(++)', namely `x'
In the second argument of `(++)', namely `x ++ " otherbit " ++ xs'
In the second argument of `(++)', namely
`" is " ++ x ++ " otherbit " ++ xs'
xs印刷には使用できますが、 「Q」の印刷"werty"には使用する必要があります。[x]何故ですか?
とは[x]どういう意味ですか?
(x:xs) では、各:要素を区切るだけxで、最初の要素も同様です。を使用して印刷できないのはなぜxですか?
またxs、どのタイプですか?値のリスト? これはx要素でxsあり、リスト型でなければならないということですか?