[Haskell-cafe] Pattern matching with where free variables can be
used more than once
Wolfgang Jeltsch
g9ks157k at acme.softbase.org
Fri Jul 17 16:19:09 EDT 2009
Am Freitag, 17. Juli 2009 20:38 schrieb Stefan Holdermans:
> Christopher,
>
> > 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!"
>
> These are called nonlinear patterns. I think Miranda (a precursor of
> Haskell, sort of) used to have them.
Yes, Miranda had them.
I see the following problem with them: Patterns are about the structure of
data. So using the same variable twice in the same pattern should mean that
the values that match the variable must have the same structure. This would
break data abstraction. For example, matching a pair of sets against the
pattern (x,x) would succeed if both sets were represented by the same tree
internally, but not succeed if both sets were equal but represented
differently.
Best wishes,
Wolfgang
More information about the Haskell-Cafe
mailing list