Difference Between Array & Tuple for Large App

AI Thread Summary
The discussion clarifies the differences between arrays, tuples, and sets in programming, emphasizing that tuples are ordered lists while sets contain distinct items without a specific order. It highlights the importance of descriptive naming conventions for variables in a large application, suggesting terms like "symbol array" or "symbol tuple" based on context. The conversation also addresses how to refer to the number of elements in these structures, with "length" being favored for tuples and "size" for arrays and sets. Ambiguities surrounding terms like "size" and "length" are acknowledged, particularly in relation to their meanings in different programming languages. Overall, the participants agree on the need for precise terminology to enhance code clarity and maintainability.
mikestampone
Messages
14
Reaction score
0
I am wondering if anyone can describe to me the correct use of a couple words. I am programming a very large application, and I like to name my variables so that they are very descriptive. Before I begin, I would like to know what is the difference between an array and a tuple? Are they the same? If I have a list of symbols, such as <B,0,1>, is it more descriptive to say "symbol array", or "symbol tuple"? Also, as all C programmers know, I will need to be naming the array size in a separate variable. Should I say "symbol tuple size", "symbol array size", or something else? What about for sets? "symbol set" and "symbol set size"? Or is there something better? Normally I would just name them whatever, but this application is gigantic, and I want to make sure I get the naming right the first time. Thanks all.
 
Physics news on Phys.org
A tuple, also called a sequence, is an ordered list of items, such as (0,1,1), which is a different tuple from (1,1,0) and (0,1).

A set is a collection of distinct items with no specified order, so {0,1} represents the same set as {1,0} and {0,1,1}.

An array is a collection of items ordered in any number of dimensions. For example, a tuple is a 1-dimensional array (it takes one index to locate an item), and a matrix is a 2-dimensional array (it takes two indices to locate an item).
 
Thank you Rasalhague :) That clears up a lot. What about the number of items in a tuple or set. Do I say count, size, cardinality, or something else. I am not sure on how to say "the number of elements" in a single word. Is it different for sets and tuples?
 
The Wikipedia entry Tuple talks about the elements and length of a tuple. The Wikipedia entry Sequence talks about the members, elements or terms of a sequence, and about its length. (The tuple article says that a tuple is a sequence; and the sequence article says that a sequence is also called a tuple.) So length looks promising - but maybe someone can give us a more definitive answer. Wolfram Mathworld doesn't go into that much detail.

I've seen several ways of defining tuples in terms of sets. For example, some sources define the tuples recursively using nested sets like this:

(a,b) = {a,{a,b}}
(a,b,c) = ((a,b),c) = {{a,{a,b}},{(a,b),c}} = {{a,{a,b}},{{a,{a,b}},c}}

or if the possibility of a 0-tuple is included:

(a,b) = {{Ø{Ø,a}},{{Ø,{Ø,a}},b}}

and then define a function as a tuple (D,C,r), where D and C are sets, the domain and codomain of the function, and r is the rule relating them. But an alternative approach defines a sequence as a function from the set {1,2,...,n} to a set S, and I don't know how a function is defined in this latter view.

This makes me hesitate to talk about the "elements" of a tuple, in case a tuple is really a set whose elements are other sets. So instead, I've been calling them "terms" or "entries" or more vaguely "items". Maybe I'm being pernickity there, and I don't suppose there's really any ambiguity, but I'm just learning about these things, and I want to get it right... Obviously the Wikipedia writer didn't see any contradiction with calling them elements or members.
 
Thanks again Rasalhague. I will use "length" throughout my program :)
 
You're welcome.

MATLAB calls it length too:

>> a=[1 1 1];
>> length(a)

ans =

3

A possible source of ambiguity, in some contexts, is that the magnitude or norm of a vector is often referred to, informally, as its length. In the case of the vector space of real coordinate vectors with the usual rules:

\text{norm}(\mathbf{a})=\sqrt{\mathbf{a} \cdot \mathbf{a}}=\sqrt{\sum_{i=1}^{n}(a_i)^2}

>> a=[1 1 1];
>> norm(a)

ans =

1.7321

i.e.

\sqrt{3}
 
Luckily, my program is not going to be using vectors, so ambiguity in this case will not be a problem. Although it will be good to consider this ambiguity in the creation of future programs which may involve vectors. Thank you for taking the time to answer my question so in-depth. I really appreciate your guidance.
 
Size is a common term for the number of elements in a collection. It's enouraged by the language too -- C++ containers use size() to get the number of elements they contain, and C has the type size_t. (Though I suppose in a few contexts, there could be an ambiguity about size referring to number of bytes -- e.g. that's what sizeof returns)

Of course, length is also common. Java uses that for its arrays, and python uses the abbreviation len for all of its container types.
 
Thanks Hurkyl. I will be using length. If I am coding with a vector data type I will default to size. It is interesting that you noted the ambiguity of size referring to a number of bytes. That is actually why I started this thread :) I was looking for something different than size, because I knew size could mean the number of bytes, in C language.
 

Similar threads

Back
Top