Comparing wildcards for equality in Haskell ..?

Is there a way in Haskell to compare that all wildcards are of the same type and value? For example, I want to create a function that exhibits the following behavior:

(1 M) (2 M) (3 M) -> True (1 S) (2 S) (3 S) -> True (1 S) (2 M) (3 S) -> False 

In other words, the first parameter must be 1, 2, and 3, and the second parameter must be all S or all M.

In this case, we can write the function as follows:

 matches (1 _ ) (2 _ ) (3 _ ) 

But how do we determine if wildcards are all S or all M?

+4
source share
2 answers

You must explicitly perform an equality check using named variables instead of wildcards:

 matches (1 a) (2 b) (3 c) | a == b && b == c = something 

(And as a note: (1 a) not a valid pattern, you need (1,a) or some other type of constructor)

+4
source

If the patterns are simple (all M or all S), why not define it as?

 matches (1, M) (2, M) (3, M) = True matches (1, S) (2, S) (3, S) = True matches _ _ _ = False 

Or are there other restrictions?

+5
source

All Articles