Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Tuple? And tuple vs. List vs. Vector?

Can you give me a brief description of Tuple? And when to use it over List and Vector?

like image 780
Truong Ha Avatar asked Jul 25 '13 08:07

Truong Ha


People also ask

Is a tuple the same as a vector?

A tuple is an object that can hold a number of elements and a vector containing multiple number of such tuple is called a vector of tuple. The elements can be of different data types. The elements of tuples are initialized as arguments in order in which they will be accessed.

What is a tuple vector?

An n-tuple vector x is defined as an ordered set of n numbers. Usually we write these numbers x1,...,xn in a column in the order indicated by their subscripts. The transpose of x is the row vector x = [x1,...,xn] and the transpose of x is x again.

What is the difference between tuple and a list?

The primary difference between tuples and lists is that tuples are immutable as opposed to lists which are mutable. Therefore, it is possible to change a list but not a tuple. The contents of a tuple cannot change once they have been created in Python due to the immutability of tuples.


1 Answers

Tuple is usually represented in Clojure through an associative data structure such as map {:name "david" :age 35} or record.

A vector ["david" 35] offers fast positional access (= 35 (nth ["david" 35] 1)), and you can store different types.

A list ("david" 35) or ("david" "justin" "david") offers fast access from the head and fast forward traversal. Although it can hold different types it would be most common for it to contain a single type, possibly containing duplicates, in a determined order. Contrast to a set #{"david" "justin"} which would contain no duplicates and is optimised for checking membership/presence.

Sorted sets (sorted-set) and maps (sorted-map) maintain the order of objects using a comparator.

Check out 4clojure and clojuredocs.org. Good luck!

like image 161
Alister Lee Avatar answered Sep 29 '22 09:09

Alister Lee