[Haskell-cafe] Pattern matching
Neil Mitchell
ndmitchell at gmail.com
Mon Jan 15 04:16:42 EST 2007
Hi Stefan,
> myFunction x@(Constructor1 _*) = ...
> myFunction x@(Constructor2 _*) = ...
myFunction x@(Constructor1 {}) =
myFunction x@(Constructor2 {}) =
> myFunction2 (Constructor1 _* a _*) = ...
>
> could be possible as long as the pattern is non-ambiguous (in this
> case, only one variable with the type of a is present in this
> constructor).
In this context a is not a type, but a value, and the non-ambiguous
constraint can get pretty confusing in a case like this. The answer is
to use records:
data Data = Constructor1 {field1 :: Bool, field2 :: Int, field3 :: Int}
myFunction (Constructor1 {field2=a}) = ...
Thanks
Neil
More information about the Haskell-Cafe
mailing list