Why do we use tuples, if we can use a two dimensional list?

后端 未结 3 589
北恋
北恋 2020-12-22 03:05

Is there a benefit to specifically reserving varied data-type pairings for tuples like such:

[(23, \"Jordan\"), (8, \"Bryant\")]

As opposed

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 03:52

    This question should have probably been more clearly stated as:

    Why must we insist on the definition of a list as a collection of elements of exactly one type?

    From one point of view the answer could be that it allows us to reason about lists in a rigorous way and a whole new class of operations become possible. Ex. how else would you define map for lists?

    From another point of view, if lists could hold multple types then the language could not have been staticly typed. Ex. if you are passed a list as an argument what is the type of head element?

    You don't have this problem with tuples because you define the type of each element a priori and the length is fixed.

    Is there a benefit to specifically reserving varied data-type pairings for tuples...

    It is absolutely necessary for a statically typed language that lists hold elements of exactly one type, so the question of benefit has no benefit.

提交回复
热议问题