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
Asked
Active
Viewed 466 times
0
-
3Related (maybe duplicate) [Haskell - Accessing a Specific Element in a Tuple](http://stackoverflow.com/q/5844347/126916) – Abhinav Sarkar Mar 28 '13 at 22:52
-
8The conventional wisdom is that if you have an 8 element tuple, it should be a datatype. – daniel gratzer Mar 28 '13 at 23:51
1 Answers
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