Possible Duplicate:
Where does the name “xs” for pattern matching come from?
I’m learning Haskell. Here is a function that calculates the sum
sum' :: (Num a) => [a] -> a
sum' [] = 0
sum' (x:xs) = x + sum' xs
I can’t figure out that xs means. x – is the head, xs – is the tail. But is it written is xs and not as just x or s?
xandxsare just variable names; you could usefredandwilmainstead, for example. But it’s very common to call the head of a listx, and the tailxs(pronounced exes), because it gives you a hint thatxis a single element, whilexsmay contain multiple elements.The example you’ve given uses pattern matching, which is a nifty feature. The line
basically says “take the input parameter to sum’, and split it after the first element. Call the first element
xand the rest of the listxs“. It would be essentially equivalent to: