Haskell Language Tuples (Pairs, Triples, ...) Extract tuple components


Example

Use the fst and snd functions (from Prelude or Data.Tuple) to extract the first and second component of pairs.

fst (1, 2) -- evaluates to 1

snd (1, 2) -- evaluates to 2

Or use pattern matching.

case (1, 2) of (result, _) => result -- evaluates to 1

case (1, 2) of (_, result) => result -- evaluates to 2

Pattern matching also works for tuples with more than two components.

case (1, 2, 3) of (result, _, _) => result -- evaluates to 1

case (1, 2, 3) of (_, result, _) => result -- evaluates to 2

case (1, 2, 3) of (_, _, result) => result -- evaluates to 3

Haskell does not provide standard functions like fst or snd for tuples with more than two components. The tuple library on Hackage provides such functions in the Data.Tuple.Select module.