pattern matching instead of prelude.head

Someone commented on StackOverflow that pattern matching the first element of a list was preferable to head. This makes sense intuitively. Could someone articulate the reason why this is true?

Pattern matching will warn you if you neglect to consider the empty list.
On Fri, Sep 16, 2011 at 10:24 AM, Michael Litchard
Someone commented on StackOverflow that pattern matching the first element of a list was preferable to head. This makes sense intuitively. Could someone articulate the reason why this is true?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 9/15/11 10:24 PM, Michael Litchard wrote:
Someone commented on StackOverflow that pattern matching the first element of a list was preferable to head. This makes sense intuitively. Could someone articulate the reason why this is true?
(1) If you call (head xs) you are assuming that xs is non-empty. If xs is empty, then it will throw an exception. Which is a major issue because tracing down these exceptions is a headache for everyone involved. Whereas, if you do (case xs of [] -> ...; x:xs' -> ...) you explicitly handle the case of xs being empty. This also applies to things like tail and fromJust. (2) Also, even if you know xs is non-empty because you called (null xs) or the like, you can do the case match once and use the results everywhere. Whereas, using head and tail means doing a case match every time they're called. This is an efficiency issue, and also applies to functions which won't throw exceptions: like fst and snd, instead of pattern matching the tuple. -- Live well, ~wren

On 11-09-15 10:24 PM, Michael Litchard wrote:
Someone commented on StackOverflow that pattern matching the first element of a list was preferable to head. This makes sense intuitively. Could someone articulate the reason why this is true?
if null s then e0 else ...(head s)...(tail s)... is a clumsy way to say case s of [] -> e0 h:t -> ...h...t... The clumsy way is more familiar because it is popularized by lisp. It is the way in lisp because lisp is old.

On Fri, Sep 16, 2011 at 2:34 PM, Albert Y. C. Lai
On 11-09-15 10:24 PM, Michael Litchard wrote:
Someone commented on StackOverflow that pattern matching the first element of a list was preferable to head. This makes sense intuitively. Could someone articulate the reason why this is true?
if null s then e0 else ...(head s)...(tail s)...
is a clumsy way to say
case s of [] -> e0 h:t -> ...h...t...
The clumsy way is more familiar because it is popularized by lisp. It is the way in lisp because lisp is old.
I have 'head :: [a] -> Maybe a' along with tail, maximum/minimum and various other prelude functions that, IMHO, shouldn't have been partial. I agree case is often better, but sometimes Maybe is convenient, e.g. when composing with other Maybes. And I can pass it easily to 'maybe', but then I suppose someone is going to say I should define 'b -> (a -> [a] -> b) -> [a] -> b' and use that directly :)
participants (5)
-
Albert Y. C. Lai
-
Evan Laforge
-
Lyndon Maydwell
-
Michael Litchard
-
wren ng thornton