0

how to reach elements of tuple by giving the index number ? Or is there any other similar way to do it? For example i have a tuple with eight element and i want to reach its 6th element

oiyio
  • 5,219
  • 4
  • 42
  • 54

1 Answers1

3

One method is to use the lens package.

> import Control.Lens
> Prelude Control.Lens> view _1 (1,2)
1
> Prelude Control.Lens> view _2 (1,2,3)
2
> Prelude Control.Lens> view _3 (1,2,3,4)
3
> Prelude Control.Lens> view _4 (1,2,3,4,5)
4

The _? operators are only defined up to _9 but it is rather easy to define more if needed.

Davorak
  • 7,362
  • 1
  • 38
  • 48