Position based tuple pattern match in Haskell

haskell, pattern-matching, tuples

Solution

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

Problem

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?

Original source