Is it possible to pattern match tuples in Haskell, but without knowing the dimension of tuple? I want to create a function which mach against any tuple, which first element is A
, like:
data A = A Int
test args@(A a,..) = a
I know there is Data.Tuple.Select
module and I can use it like this:
test args = case sel1 args of
A a -> a
...
But is this the only way of doing this or has Haskell got some default mechanisms to match against any dimension tuple?
You could use the ViewPatterns
extension to pattern match the result of a function applied to an argument:
{-# LANGUAGE ViewPatterns #-}
data A = A Int
test (fst -> A a) = a
You could use lenses to project out arbitrary fields:
{-# LANGUAGE ViewPatterns #-}
import Control.Lens
import Control.Arrow ((&&&))
data A = A Int
test (fields _1 _3 -> (A x, A y)) = x + y
fields f1 f2 = (^.f1) &&& (^.f2)
-- > test (A 1, A 2, A 3)
-- > 4
If you don't want to use type classes, you could also use nested tuples. So instead of having a tuple of type (A, B, C, D)
you have a tuple of (A, (B, (C, D)))
.
You could then easily match against the first element of a however deeply nested tuple, like this:
test :: (A, b) -> Int
test (A a, _) = a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With