Triple access element

If I had a motorcade in Haskell:

x = (1, 2)

I could use fst x to get 1 and snd x to get 2

I was wondering if I had a triple:

y = (1, 2, 3)

Is there a similar function that I could use?

+5
source share
3 answers

You need to write your own extractor functions:

extractFirst :: (a, b, c) -> a
extractFirst (a,_,_) = a

Functions fstand are sndapplicable only for a set ie(a, b)

+6
source

. , 1of2, 2of2, 1of4, 4of7. , , fst snd , :

4of7 (_, _, _, x, _, _, _) = x

lens, , .

3of4 = view _3
4of4 = view _4
3of7 = view _3
...

.

+6

As already mentioned, the way to go is pattern matching. However, for completeness, I wanted to mention that you can just use the lambda function:

head $ map (\(a, b, c) -> a) (y : [])
+3
source

All Articles