[Haskell-cafe] Disjunctive patterns

Holger Siegel holgersiegel74 at yahoo.de
Thu Dec 8 14:47:04 CET 2011


Am 08.12.2011 um 11:13 schrieb Asger Feldthaus:

> Haskell doesn't seem to support disjunctive patterns, and I'm having a difficult time writing good Haskell code in situations that would otherwise call for that type of pattern.
> 
> 

> In Haskell I can't find any equivalent to the disjunctive pattern. If expanded naively, my Haskell version would look like this:
> 
> foo :: T -> T -> Int
> foo x y = case (x,y) of
>   (Foo a, Foo b) -> a + b
>   (Foo a, Bar b) -> a + b
>   (Bar a, Foo b) -> a + b
>   (Bar a, Bar b) -> a + b
>   (Baz, Foo a) -> -a
>   (Bar a, Baz) -> -a
>   (Baz, Bar a) -> a
>   (Foo a, Baz) -> a
>   _ -> 0
> 
> While my example is still managable in size, this quickly goes out of hand in practice. I've tried using pattern guards but with limited success.

value :: T -> Int
value (Foo a) = a
value (Bar a) = a
value Baz = 0

foo :: T -> T -> Int
foo Baz (Foo a) = -a
foo (Bar a) Baz = -a
foo x y = value x + value y




More information about the Haskell-Cafe mailing list