Reputation: 3113
How should I define tuple type Quartet
, that will contain four elements – two Integral
s and two Boolean
s, and functions firstTwo
returning table of Integrals and secondTwo
returning table of two Booleans?
data Quartet = Quartet (Int, Int, Bool, Bool)
--firstPair :: Quartet -> [Int, Int]
firstPair (a, b, _) = [a, b]
--secondPair :: Quartet -> [Bool, Bool]
secondPair (_, _, a, b) = [a, b]
Upvotes: 1
Views: 145
Reputation: 77504
As you have defined it, the Quartet
value constructor takes one argument, which is a 4-tuple. You could also define it like this:
data Quartet = Quartet Int Int Bool Bool
and it will work pretty much the same except the pattern matching will be slightly different.
You can also make a type synonym for 4-tuples of this particular type:
type Quartet = (Int, Int, Bool, Bool)
If you opt for this final approach, your functions will work just as you've written them, because pattern matching in this last case is nothing more than matching against a plain tuple.
However, if you use either of the first two cases, the key idea is that in your function definition, you have to pattern match the value constructor, so you need to include the Quartet
value constructor as part of the pattern:
--firstPair :: Quartet -> [Int, Int]
firstPair (Quartet (a, b, _, _)) = [a, b]
--secondPair :: Quartet -> [Bool, Bool]
secondPair (Quartet (_, _, a, b)) = [a, b]
Upvotes: 3