Is there a difference between a list and a tuple?

风流意气都作罢 提交于 2019-12-10 13:49:17

问题


I see existing questions that relate to specific programming languages. There are implementation differences in specific languages, but is there a theoretical conceptual difference?

Mutable vs immutable: In Python, lists are fully mutable while tuples are immutable or persistently immutable so that modifications create new tuples and do not do in place modifications. But this is purely an implementation detail. In other languages tuples are mutable and lists are immutable.

Heterogeneous vs homogeneous: Semantically, tuples are usually heterogeneous, while lists are usually homogeneous, but this is more of a convention and there are many exceptions. Dynamically typed languages like Python have heterogeneous lists. Haskell, for example, has support for fully statically typed heterogeneous lists called HList.

Finite vs Infinite: Theoretically, a list can be infinite, and some programming languages (Haskell) support infinite lists. A tuple can not be infinite.

UPDATE: The only theoretical difference is that a tuple must be finite, while a list can theoretically be infinite. The rest of the differences are pure implementation differences.

Wikipedia says "A tuple is a finite ordered list of elements.".

This makes it clear that a tuple is a list, but a finite list.


回答1:


From a C# perspective, the most glaring difference would be that Tuples are fixed length over the duration of their existence, whereas Lists support add and remove functionality that may cause their lengths to change.

You could argue this is just an arbitrary implementation decision, but this leads back to the mutability issue. Say I have a Tuple<double, double> to represent a 2D point. If I remove one of the elements so I have Tuple<double>, it is clear that this is no longer a 2D point and the original meaning of even the remaining dimension is likely no longer relevant or usable.

If, however, I had a List<double> representing 2 students' scores, and remove one, I now have a list of 1 student's score. But the one remaining double is still a score and still retains the full meaning / relevance of a score.

In short, I see Tuple elements as attributes or dimensions (usually the minimally required defining set), whereas I see List elements as arbitrary instances.




回答2:


Mathematically, a tuple might be a list of elements and therefore also a list, but I don't know about the specifics of tuple and list in that domain. From a programming point of view I see a semantic difference which is that the list is a container for elements and the tuple is an object that represents multi-dimensional data (2D or 3D points for example). So you would not use a tuple to save a list of elements. Instead you use a tuple to represent multi-dimensional data.




回答3:


A tuple is a subtype of a list with additional type constraints for each of the elements and a defined size as part of its type. If a list has size constraints and specifies the types of all its elements, (like Haskell's HList) then it is also a Tuple conceptually.

Not all lists are tuples, but all tuples are lists.

The relationship between a tuple and a list is the one between negative numbers and numbers. Not all numbers are negative numbers, but all negative numbers are numbers.




回答4:


A tuple is just a finite list. All tuples are lists. All finite lists are tuples. Infinite lists are not tuples. All differences of typing and semantics are purely language dependent considerations.



来源:https://stackoverflow.com/questions/30518013/is-there-a-difference-between-a-list-and-a-tuple

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!