<div dir="ltr"><div>I am reading Learn you a Haskell for great good and on page 40 - as-patterns.</div><div><br></div><div>I have changed the example slightly to be:</div><div><br></div><div>firstLetter :: String -> String</div>
<div>firstLetter "" = "Empty string, oops"</div><div>firstLetter all@(x:xs) = "The first letter of " ++ all ++ " is " ++ [x] ++ " otherbit " ++ xs</div><div>Then can use like this:</div>
<div><br></div><div>*Main> firstLetter "Qwerty"</div><div>"The first letter of Qwerty is Q otherbit werty"</div><div>But I was confused about the difference between [x] and x and why I have to use [x] in the above example.</div>
<div><br></div><div>For example if I change to</div><div><br></div><div>firstLetter :: String -> String</div><div>firstLetter "" = "Empty string, oops"</div><div>firstLetter all@(x:xs) = "The first letter of " ++ all ++ " is " ++ x ++ " otherbit " ++ xs</div>
<div>I get error:</div><div><br></div><div>Couldn't match expected type `[Char]' with actual type `Char'</div><div>In the first argument of `(++)', namely `x'</div><div>In the second argument of `(++)', namely `x ++ " otherbit " ++ xs'</div>
<div>In the second argument of `(++)', namely</div><div> `" is " ++ x ++ " otherbit " ++ xs'</div><div>I can use xs to print "werty" but have to use [x] to print "Q". Why is that?</div>
<div><br></div><div>What does [x] mean?</div><div><br></div><div>In the (x:xs) : just delimits each element. so x is the first element. Why can I not print by using x?</div><div><br></div><div>Also xs is of what type? list of values? So does this mean x is an element and xs must be of type list? Confused...</div>
<div><br></div></div>