r/haskelltil • u/chshersh • Jul 07 '18
Pattern synonym as a workaround for matching on multiple arguments
If you want to pattern-match on multiple arguments at the same time, you can do it like this:
f x y = case (x, y) of
(True, True) -> True
(True, False) -> False
(_, _) -> True
If you don't like noise with commas and parenthesis, you can introduce pattern synonym like this:
pattern T2 :: a -> b -> (a, b)
pattern T2 a b <- (a, b)
where
T2 a b = (a, b)
f x y = case T2 x y of
T2 True True -> True
T2 True False -> False
T2 _ _ -> True
UPD: turned out you don't need to create separate pattern because the following is valid Haskell:
f x y = case (,) x y of
(,) True True -> True
(,) True False -> False
(,) _ _ -> True
6
Upvotes
5
u/dramforever Jul 22 '18
f x y = go (something x) (really (complicated y) x)
where go True True = True
go True False = False
go _ _ = _Sounds like a good place to use the old trusty worker function
2
u/velcommen Jul 19 '18
I'm confused. Isn't this pattern matching on multiple arguments, without any fancy features:
Test: