I'm trying to pick up a bit of Haskell, and I'm alternating between awe and befuddlement. One of the really alienating things for me, trivial as this may seem, is the pattern matching idiom (x:xs)
. Where do those variable names come from? They could be anything -- (kernel:cob)
, (spam:eggs)
(tipping my hand a bit), or -- most sensibly, to my mind, (h:t)
, standing for 'head' and 'tail'.
I suppose the x
prefix is useful for indicating that both items come from the same list, so then (xh:xt)
or even just (x:xt)
if you're feeling especially terse. But why s? What does it mean? Where did it come from? I feel, at the moment, that knowing would help me cope with my confusion.
Perhaps I am thinking about this in the wrong way; please feel free to tell me so.
(x:xs) just means a single x is attached as the head of a list of xs (essentially, sort of conveying the message that it is a list containing similar x elements).
x:xs represent a list which x is the first element (head) and xs is the rest of the list (tail). This pattern is commonly found in pattern matching of a function that has list as argument along with [] (empty list).
x
is a common variable name in mathematics. xs
is the plural form of x
(get it?). In list pattern matching, x
is one element, and xs
is (generally) several.
We can take the origins of x
back much further, of course :-) François Viète (1540-1603) produced a book titled In artem
analyticem isagoge (“Introduction to
the Analytic Art”), which was the first work that systematically used letters to represent numbers.
Edit: Porges, below, points me to the really cool A History of Mathematical Notations by Cajori. To my pleasant surprise, the entire work is available online: http://archive.org/details/historyofmathema031756mbp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With