
Hi I sometimes have a function definition similar to this: myFunction x@(Constructor1 _ _ _ _ _ _) = ... myFunction x@(Constructor2 _ _ _ _ _ _ _ _) = ... which in my eyes is not very elegant and easy to type. Is there an easier way to switch on the different constructors of a type (Data.Generics certainly could be used..)? If not, has thought been put in a pattern matching possibility of the form _*. This would allow: myFunction x@(Constructor1 _*) = ... myFunction x@(Constructor2 _*) = ... Also pattern matches of the form: 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). regards Stefan

On Mon, Jan 15, 2007 at 10:09:10AM +0100, Stefan Aeschbacher wrote:
Hi
I sometimes have a function definition similar to this:
myFunction x@(Constructor1 _ _ _ _ _ _) = ... myFunction x@(Constructor2 _ _ _ _ _ _ _ _) = ...
myFunction x@Constructor1{} = ... myFunction x@Constructor2{} = ... this works even for non-record types

2007/1/15, Stefan O'Rear
On Mon, Jan 15, 2007 at 10:09:10AM +0100, Stefan Aeschbacher wrote:
Hi
I sometimes have a function definition similar to this:
myFunction x@(Constructor1 _ _ _ _ _ _) = ... myFunction x@(Constructor2 _ _ _ _ _ _ _ _) = ...
myFunction x@Constructor1{} = ... myFunction x@Constructor2{} = ...
this works even for non-record types
I didnt know about the possibilty to use {}. This solves my problem quite nicely. thanks

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
participants (3)
-
Neil Mitchell
-
Stefan Aeschbacher
-
Stefan O'Rear