[Haskell-cafe] Pattern matching with where free variables can be used more than once

Christopher Done chrisdone at googlemail.com
Fri Jul 17 14:35:10 EDT 2009


Wouldn't it be great if pattern variables could be used more than once
in a pattern? Like so:

    foo [x,x,_,x] =  "The values are the same!"
    foo _      = "They're not the same!"

where this could be rewritten to:

    foo [x,y,_,z] | x == y && x == z = "The values are the same!"
    foo _      = "They're not the same!"

It seems like a straight-forward translation and there wouldn't be a
speed hit for normal patterns because it would only be triggered in
compilation where the same free variable appears twice.

Implications are:

1. in ``foo [x,y] = ...'', x has type a
1. in ``foo [x,x] = ...'', x has type Eq a => a

Was this ever considered? Is it a bad idea for some reason I'm not aware of?

On a mildly irrelevant note, I have observed newbies to the language
wonder why on earth it's not already like this.

Cheers


More information about the Haskell-Cafe mailing list