Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a Specific Element in a Tuple

People also ask

How do you access a tuple in a tuple?

Indexing We can use the index operator [] to access an item in a tuple, where the index starts from 0.


What prevents the language from having the special construct you want is its design. The designers just didn't put this in, because it would complicate the language definition, which is quite minimalistic. fst and snd are library functions for the common case of pairs; you can define all the others yourself, or better, define record types for your data so that your data members have appropriate names.

(It may be that GHC has an extension to do this, but I haven't encountered one; check the docs or ask on the mailing list to be sure.)


Check out the tuple library on hackage. It has overloaded functions for various operations on tuples (up to a predefined size).


N-tuples are not a data structure for indexing via an Int key, instead, you should look at one indexed-biased data structures, such as arrays or finger-trees.

Now, one could imagine writing a typeclass for a family of tuple types providing an index operation, however, we already have arrays for that, and there's a lot of boilerplate necessary to make tuples of any type seamlessly provide this operation. The power gained isn't worth the effort.


Why can't this be done easier? Or maybe there is some easy way?

It can be easier using a recent alternative the lens package. The Tuple module has selectors for up to 9 element tuples and it is straight forward to define more if needed.

> import Control.Lens
> data A = A deriving (Show)
> (1, '2', "3", A) ^. _1
1
> (1, '2', "3", A) ^. _2
'2'
> (1, '2', "3", A) ^. _3
"3"
> (1, '2', "3", A) ^. _4
A

You can also use the lens package to update elements polymorphically, change type on update.

With and without infix operators:

> (1, '2', "3", A) & _1 .~ "wow"
("wow",'2',"3",A)
 > set _1 "wow" (1, '2', "3", A)
("wow",'2',"3",A)

The github readme is a good place to start to find out more about the underlying theory as well as numerous examples.


Not just tuples

Similar syntax works for Traverables and Foldables, so Trees, Maps, Vectors, etc. For example if I had a list of tuples I can access the third tuple element at the 1 index by composing the element 1 to access the first index element with _3 to access the third tuple element.

[(1,2,3),(4,5,6),(7,8,9)] ^? element 1 . _3
Just 6