Why are we using tuples when we can use a two dimensional list?

Is there an advantage to specifically reserving different datatype pairs for tuples like this:

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

      

As opposed to a simple 2D list:

[[23, "Jordan"], [8, "Bryant"]]

      

I know the second piece of code will not work in Haskell

+1


source to share


3 answers


Why are we using tuples when we can use a two dimensional list?

Since lists and tuples are conceptually different, the type system provides us with a useful way to indicate and recognize differences in code. For one of many possible examples, one might define ...

type ListyPair a = [a]

      

... and then...

listyFst :: ListyPair a -> a
listyFst [x, _] = x

listySnd :: ListyPair a -> a
listySnd [_, y] = y

      



... so that:

GHCi> listyFst [3,4]
3
GHCi> listySnd [3,4]
4

      

But what happens if the "pair" list has only one item or not? We would have to throw a runtime error (yuck) or do listyFst

and the listySnd

result Maybe a

so that we can work fine with the error. What if the "pair" has more two elements? Should we just discard them silently, or would it be better for the functions to fail in this case?

From the point of view of a strong user of the system, when we replace an actual pair ListyPair

, we are discarding useful information. Knowing that there are really only two elements, we can avoid all of the above complications.

realFst :: (a, b) -> a
realFst (x, _) = x

realSnd :: (a, b) -> b
realSnd (_, y) = y 

      

+3


source


"Is there an advantage to specifically reserving different datatype pairs for such tuples, as such:"

Yes. The benefits of using tuples are related to data usage patterns and any performance requirements.

Tuples are often immutable and of a predictable size.

Since you are writing a haskell program, your lists of "strings and integers" are actually a union type list (call it what you want) ... your tuple is just a special case.

This point is a little clearer if you think of it as python code. (Both lines you give are valid python code, originally I thought the question was python related)

In python, you would prefer a list of tuples if you knew that tuples have a fixed (or predictable) structure.



This will allow you to write code like:

[ print(name) for jersey, name in list_of_tuples ]  

      

You would prefer to use a tuple if you think it is not worth paying for the cost of representing an object, and you would prefer to express each set reference with unboxing.

Fortunately, since you are writing haskell, you have many tools for modeling and representing problems in the type system. I think if you take a little time to read and write some hackell (find out that Haskell is a great book for Great Good), you will have a better understanding of the type system, and you will find the answer to this question yourself.

If you want to learn more about functional programming in general, The Little Schemer is impressive too.

((un) Fortunately, in python, you can write code that handles both types of data structures, rather than worrying about the types that go into the code. Haskell requires you to think a little differently than that.)

+1


source


This question should have been more clearly stated as:

Why should we insist on defining a list as a collection of items of only one type?

From one point of view, the answer might be that it allows for strict reasoning about lists and a whole new class of operations becomes possible. Ex. how else would you define map

for lists?

On the other hand, if lists can contain multiple types, then the language cannot be statically typed. Ex. if you passed a list as an argument, what is the type of the 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 it possible to specifically reserve different data pairs for tuples ...

It is absolutely imperative that a statically typed language maps retention elements of exactly the same type, so the question of benefit is of no use.

+1


source







All Articles