[Haskell-cafe] All equations must have the same arity - why?

ajb at spamcop.net ajb at spamcop.net
Sun Jan 13 18:53:01 EST 2008


G'day all.

Quoting Neil Mitchell <ndmitchell at gmail.com>:

> It's nice to write functions in point free style:
>
> f = sort . nub
>
> But sometimes I have to add an extra case, on a certain value:
>
> f [] = [1]
> f = sort . nub
>
> But now these equations have different arities, and its rejected by
> Haskell.

I don't know why Haskell does it specifically, but here's why I like
the behaviour:

1. Equations with different arities more often signal bugs than
correct intentions.  One common situation is adding an argument to
a function, and accidentally leaving one equation out.

2. It's highly misleading.

This:

     f [] = [1]
     f = sort . nub

suggests that f is (sort . nub), which is most decidedly is not.

By forcing you to add the argument:

     f [] = [1]
     f xs = sort . num $ xs

it reminds you more strongly that what you're actually writing is a
shorthand for this:

     f [] = [1]
     f xs@(_:_) = sort . num $ xs

Cheers,
Andrew Bromage


More information about the Haskell-Cafe mailing list