[Haskell-cafe] pattern matching instead of prelude.head

Evan Laforge qdunkan at gmail.com
Sat Sep 17 01:22:29 CEST 2011


On Fri, Sep 16, 2011 at 2:34 PM, Albert Y. C. Lai <trebla at vex.net> wrote:
> 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 :)



More information about the Haskell-Cafe mailing list