Haskell/Solutions/Pattern matching

Matching literal values
1.

When used for pattern matching, a simple variable name like the  in

binds  to the argument of   and, crucially, matches anything at all. Since we are not using the bound  variable in the right hand side of the equation, in this function the effect of this definition is the same of

Which is why GHC/GHCi complains about overlapping patterns, and why the second equation for  gets ignored. Also, the  definition made outside of the function has no influence on what happens - the   used in pattern matching has local scope (that of the   equation), and has nothing to do with that other.

2.

is not a literal value; rather, it is an algebraic datatype like the ones we first met in the previous chapter. In essence, its definition in Prelude amounts to simply:

and  are parameterless constructors; and that is why their names start with a capital letter.

Syntax tricks
You probably used  instead of an as-pattern in the first version of this exercise; here, on the other hand, we do not match the empty list in the pattern for. In this specific case, neither solution is actually unsafe, because the result of  will never be an empty list. Nevertheless, pattern matching still is a better choice than, as it makes it more obvious that we chose not to handle the empty list case.