Why are tuples of arbitrary size useful? (Haskell Template)

In the introductory text for Template Haskell, one example of why Template Haskell is useful works with arbitrary tuple sizes.

What is the purpose of an arbitrary size of tuples? If the data type is the same, why not use a list? And if the data types in the tuple differ, how can it be expanded to arbitrary sizes?

+4
source share
1 answer

With an arbitrary value, means at compile time . Therefore, if you want tuples with fifteen elements, the Haskell template will generate a function for the tuple with fifteen elements. However, after compilation, the number of elements is fixed. The advantage of using a tuple is that you can access each element at a constant time O (1). Thus, you can use a type system to enforce a tuple that still contains a fixed number of elements.

In addition, selin the example it can work with tuples, where elements are of arbitrary types . For example, it sel 2 3will generate a function:

$(sel 2 3) :: (a,b,c) -> b
$(sel 5 5) :: (a,b,c,d,e) -> e

[a], , :

(!!3) :: [a] -> a

a. , , . , ( ).

A list, , . - [Int] - , , . , k- O (k) . , , , , .

+12

Source: https://habr.com/ru/post/1624268/


All Articles