38

A container having 2 or more elements could be called a "plural container". A container with no elements could be called an "empty container".

But what is the terminology for a container whose current size is exactly 1 element?

I would use the term "unary" but that refers to operators that take exactly 1 argument.

The reason I would like a term for this is that I am writing a boolean method to check if two data structures are both of size exactly one and I want a good name for the method. Currently I have:

def length_is_1(self, x, y):
    return len(x) == 1 and len(y) == 1
Glorfindel
  • 3,137
  • 3
    degenerate container? – πάντα ῥεῖ Aug 25 '20 at 14:42
  • If you're satisfied with "plural container" for the one concept, then why not just call it "single element container"? Seems pretty straight-forward. – Eric King Aug 25 '20 at 15:14
  • 1
    If it were only designed to hold one element or none, you could use the word full. But if you have a list that is capable of holding zero or many, you can describe the item itself as "lone", but I'm not sure there is a specific word that describes the status of a general purpose container as "currently containing only one item". I think if you were determined, you'd have to adapt a term such as "singular", "simple" or "primal", perhaps even "scant", but I don't think there's an existing term that meets the requirement head-on. – Steve Aug 25 '20 at 15:35
  • @πάνταῥεῖ interesting remark! A degenerated list, is a strange list. A strange list is a singular list. And a singular list is not plural ;-) – Christophe Aug 25 '20 at 16:03
  • @Christophe: And a degenerate tree is a list! – Jörg W Mittag Aug 25 '20 at 16:16
  • @JörgWMittag So true! Excellent! And the tree shall not hide the forest. You made my day :-) – Christophe Aug 25 '20 at 16:21
  • 3
    In SQL we speak of a "Singleton" Selection, and there is an answer for this. – mckenzm Aug 26 '20 at 03:00
  • @Steve a container that can only hold 0 or 1 item is going to essentialy be Option/Maybe type – jk. Aug 26 '20 at 07:15
  • len(x)*len(y)==1 :) – 12Me21 Aug 26 '20 at 10:16
  • "box"/"boxed", as opposed to "unboxed"? – Filip Haglund Aug 26 '20 at 10:32
  • 11
    Frame challenge: Why are you writing a function to check that two argument lists have exactly one element? This seems quite overspecific. There's no abstraction in that function. Either this function represents something that can be termed in the context of its intended use, or it should not be a function at all, imho. – cmaster - reinstate monica Aug 26 '20 at 10:35
  • 1
    @12Me21 - I really, really wanted to say that in at least one language I've used, a list with no elements had a len of 0, but an uninitialized list had a len of -1, and -1 * -1 = 1, but do you know, I cannot find any evidence at all to support that. I am truly getting old and senile. – Spratty Aug 26 '20 at 11:59
  • @jk., I'm not myself fond of the noun terms "an option" or "a maybe" as the description for a container which can be full or empty. You wouldn't describe in English, say, a bowling ball bag, as belonging to a category of "option" or "maybe" containers. These terms suit contexts where the emphasis is purely on the presence or absence of a value, not when the emphasis is on the presence and description of a containing structure. That is, a container "whose contents are optional", does not make the container itself "an option" - at least not in the English language as we know it. – Steve Aug 26 '20 at 12:31
  • 8
    Method names: HasSingleItem, ContainsSingleItem, NotPluralOrEmpty. Inspired by linq and string methods names in C#. – Sinatr Aug 26 '20 at 14:23
  • @cmaster-reinstatemonica : the reason is that the cases of algorithm branch into different logic based on whether the 2 lists of have 0, 1 or 2 elements. And a certain case is based on both lists having exactly 1 element. – Terrence Brannon Aug 27 '20 at 08:09
  • ...Unwrappable? – Jared Goguen Aug 29 '20 at 10:18
  • It's a list. It happens to be non-retrogradable as well, but who cares? It's a list. – dmedine Sep 23 '20 at 00:26
  • Why do you need a name for this? In the case of, for example, a linked list it is common practice to pop elements off until the list is empty. From a procedural point of view, it is irrelevant how many elements there are whether it be 0, 1, or N, so why do you need a name? As other posters point out, the terms 'singleton' and 'plural' imply a static membership of 1 or 2 elements. – dmedine Sep 23 '20 at 00:32
  • 1
    IS_Length_One(self, x, y) sounds better – demoncrate Sep 23 '20 at 11:09
  • I agree that they key here is understandability. Besides, I recommend functions that return true/false to start with a verb, so I would use is_length_1, is_count_1, has_one_element, are_one_item_lists, etc. What you may want to review is if it's better for you to use singular or plural verbs, maybe plural fits better. – Andrew Sep 25 '20 at 05:45
  • 1
    This is a solution looking for a problem to solve. – Caltor Sep 25 '20 at 10:04
  • 1

10 Answers10

64

Do not come up with a new word. Your name is perfectly fine: It is unambiguous and specific and consequently leaves no doubt to the reader what you are talking about.

By contrast, singleton, unary, 1-tuple or any other term borrowed from mathematics or software engineering carries with it a baggage of preconceptions which are confusing. A list with a single element in it is emphatically not a singleton. It has nothing to do with unary operators, and a list is clearly not a tuple in C++. It is a list with one element in it, not more, not less.

That is sometimes a perceived downside of a simple approach in programming: It seems unsophisticated, and hence of a lesser value. Look at Duff's device! Marvel at the ingenuity of boost.lambda! But then listen to Jim Radigan, who leads the VC++ compiler team:

One of the other things that happen when we go to check code into the compiler is we do peer code review. So if you survive that, it’s probably ok, it’s not too complex. But if you try to check in meta-programming constructs with 4-5 different include files and virtual methods that wind up taking you places you can’t see unless you’re in a debugger – no one is going to let you check that in.

That your peer is able to understand your code right away because it is plain and simple and calls things what they are is not a sign that you didn't realize your full programming potential. It is a sign of excellence. Do not look for a Latin word.

  • 11
    "Do not come up with a new word" – I 100% agree. The Java API calls this a singleton list, and Java is one of the most widely used programming languages. So, don't come up with a new word, just stick with what Java already uses. – Jörg W Mittag Aug 26 '20 at 09:26
  • 8
    @JörgWMittag The Java API calls that a singleton if only exactly one element can be in the list. That "Highlander paradigm" is not at play in the OP's case. Calling a general list which just happens to have one element a singleton is plain misleading. – Peter - Reinstate Monica Aug 26 '20 at 09:42
  • 1
    @Peter-ReinstateMonica According to this logic you may only call a list without elements an empty list if it is immutable, as the list provided by Collections.emptyList() is immutable. Immutability is just a completely unrelated property regarding this terminology. – Lukas Körfer Aug 26 '20 at 11:34
  • 2
    @LukasKörfer "Empty list" is a general term in computer science, denoting what it says. The contraction used to name Collections.EmptyList is derived from this general term. By contrast, at least to me singleton in software engineering is a specif term used by the gang of four to name the singleton pattern. Yes, it has usages outside this specific pattern, and even outside software, but if in the context of software somebody tells me "this is a singleton" I have a very specific idea of what they mean, and it is not a general list which just happens to have a single element in it. – Peter - Reinstate Monica Aug 26 '20 at 11:47
  • @LukasKörfer This is, obviously, just my perception and experience; different groups of people and speakers from different regions use words differently. Your mileage may vary. Language is also dynamically evolving. In the end t is hard to be sure about anything with natural languages. My answer reflects my perception: That any of these words would be pretty substantially mislead (me). That said, typically I'm not the only one when it comes to language. I would bet that in a room of 10 programmers another one would think "singleton! Ah. I know *that" and be on a wrong trajectory. – Peter - Reinstate Monica Aug 26 '20 at 11:52
  • 1
    @Peter-ReinstateMonica Just because the term empty list is more common it does not mean that singleton list is less precise. As other answers have already stated, there is no common or widely accepted term for a list with exactly one element, but the term singleton list or singleton set is pretty descriptive and may be used to express this context. Whether or not this list is immutable is completely unrelated to this terminology. – Lukas Körfer Aug 26 '20 at 11:55
  • 2
    @Lukas I find the immutability essential: Singleton implies there can be only one. – Peter - Reinstate Monica Aug 26 '20 at 11:56
  • 6
    In a mutable (programming) world we can only describe the current state. When you call a list an empty list, you would only describe that one moment where it is actually empty. Once you added an item it is not empty anymore. Nevertheless we call lists (regarding to their current state) empty lists. In the same way we might call lists with only one item (regarding to their current state) singleton lists. – Lukas Körfer Aug 26 '20 at 12:00
  • 2
    @LukasKörfer "In a mutable (programming) world we can only describe the current state": This is exactly the core issue.The word singleton describes all possible future states of the program: There will always be exactly one object (in that list, or in the entire program). We can obviously describe much more than "only the current state": We can make assertions about future states, and one such assertion is that a singleton will never have a second instance next to it. This cannot be said about a general list of size 1. It can be said, however, about java.util.Collections.singletonList. – Peter - Reinstate Monica Aug 26 '20 at 12:11
  • 2
    @Peter-ReinstateMonica Now we are at the point we need to decide whether we want to interpret the word singleton based on its original meaning or based on an (anti-)pattern with the same name. – Lukas Körfer Aug 26 '20 at 12:29
  • I disagree that singleton should be confined to use as a particular type of list (must contain one element) or confined to correspond to math uses. Context is everything. As long as it makes the meaning clear, it's perfectly fine for some context to use singleton to mean a list or set or ... that has a single element. Think of a game of bridge. – Drew Sep 23 '20 at 17:18
  • 1
    I agree with not trying to come up with a new word, but personally, I would just go with "single-item list". I think that's more clear than "singular list" (since "singular" can mean the opposite of plural, but it can also mean "extraordinary; remarkable; exceptional"). – devuxer Sep 24 '20 at 05:36
  • 1
    @devuxer Also, the "singular" in "singular list" is an attribute of list; but the list is not singular at all. "Singular item list" is much better. Or "single item list". Or "list_of_length_1" ;-). – Peter - Reinstate Monica Sep 24 '20 at 06:47
  • Excellent answer (and I agree), but I'll add a small caveat: if problem domain has a word for it, then ok to use it. E.g. if you're writing mathematics set theory software library, then such library is already likely to make heavy use of math terminology (cardinality, compliment, cartesian product, etc), then it's ok to use terms from that domain - "singleton set" in this case - and having a class Set with a member Set.IsSingleton right along Set.cardinality and Set.IsNull as the term would be expected to be interpreted in that context domain. – LB2 Sep 25 '20 at 03:45
30

There is as far as I know, no widely accepted term in SW engineering to described a list with exactly one element

In mathematics, and more precisely in the set theory, a set with only one element is called a singleton. Unfortunately, in SW engineering, the term is so heavily associated with a design pattern, that using it for another purpose might create a confusion.

Anyway, a list is ordered, so a set is not a list. An ordered list of items is called a sequence in mathematics. And a sequence with exactly one element is called a 1-tuple. Unfortunately, a tuple corresponds to specific data structures, so this term, even converted to letters-only, would also be very ambiguous.

Finally, if mathematics don’t help, let’s look at literature! The contrary of plural is singular. According to the Meriam-Webster dictionary, it means “of, or relating to, one person, thing or instance”. So a singular list should express exactly what you want. This term is even reinforced by its ambiguity: “singular” is associated in other contexts with strangeness, and a list with only one element is indeed a strange list (as πάντα ῥεῖ already pointed out in the comments. But I’d nevertheless prefer to explain it a comment at first use ;-)

Christophe
  • 77,139
  • 16
    I'm not sure how representative the Java SE API is for the entire world of software engineering, but they have been calling it a singleton list since 2000. – Jörg W Mittag Aug 25 '20 at 15:56
  • @JörgWMittag Good point! Glad to see that the idea was not as far-fetched as I initially thought. On the other side, I’m not sure how representative the GoF is for the entire world of software engineering, but they called something else singleton since 1994, and 10 times out of 10 when I encounter this term nowadays in a SW context, it’s for the design pattern ;-) – Christophe Aug 25 '20 at 16:09
  • I use Ruby, which has singleton classes (each object has a class of which it is the only instance, thus this class can hold instance-specific behavior) and singleton methods (methods that only exist on one instance, i.e. methods of the singleton class), and Scala, which has singleton types (for any stable identifier foo, the type foo.type denotes the type only inhabited by that single object, e.g. a method whose return type is this.type can only return this and nothing else), so my experience is actually much different. – Jörg W Mittag Aug 25 '20 at 16:15
  • 1
    May be worth adding that “singular” is also the obvious contrast to “plural” as suggested in the question for 2-or-more-element lists. – KRyan Aug 26 '20 at 01:52
  • 2
    Regarding the GoF pattern, in Java "singleton" still does mean that (though more usually in practice a container-managed singleton), but singleton is commonplace. – chrylis -cautiouslyoptimistic- Aug 26 '20 at 04:00
  • 7
    To me, "a singular list" sounds like it means there's only one list being considered. The dictionary definitions for the word also include "Being only one of a larger population" and "of or relating to a separate person or thing / ... a single instance or to something considered by itself". Is there something wrong with just "a one-element list"? (Also, context probably matters in that if it's possible to define the terms used which gives some leeway.) – ilkkachu Aug 26 '20 at 04:41
  • 1
    @JörgWMittag: But a "singleton list" in the Java API is an immutable list of length one. I think it would be rather surprising for an API to use the term "singleton" to mean only that a collection currently has exactly one element. – ruakh Aug 26 '20 at 07:26
  • @ilkkachu human languages are great for their flexibility, so your argument is valid. This is why I suggested to add a comment at first use. But if I’d speak about one list, I would say “single list” and not artificially complexify my sentence using longer words. Moreover, if “a singular word” is a word about one thing, a “singular list” should in principle be a list about one thing. isSingular seems therefore a fair pendant to isPlural and isEmpty. Of course, I do not want to exclude the longer less ambiguous form that you propose, which seems a good idea, despite made of several terms – Christophe Aug 26 '20 at 07:51
  • 1
    I think, the 1-tuple is actually a very good term. Much better than singular or plural list, which my brain refused to parse at first. Maybe the best term would be to simply say 1-list. – cmaster - reinstate monica Aug 26 '20 at 10:31
  • 1
    @ruakh Immutability is just an unrelated property regarding this terminology. Collections.emptyList() returns an immutable list, but the term empty list does not imply that every empty list is immutable. – Lukas Körfer Aug 26 '20 at 11:45
  • @cmaster-reinstatemonica : 1-tuple is somewhat OK, but python has both tuples and lists and I'm interested in lists... the term singleton could apply to either "container" (to use generic terminology) or Iterator (to use Python terminology). – Terrence Brannon Aug 27 '20 at 08:12
  • 1-tuple is also called a single (double, triple, quadruple, quintuple, sextuple, septuple, ..., n-tuple). – Lie Ryan Sep 27 '20 at 04:43
20

The term you are looking for is singleton.

In mathematics, a set of cardinality 1 is called a singleton set or just singleton. A Tuple of length one is sometimes called a singleton as well.

By analogy, collections of size 1 are typically also called singleton collections in programming, see for example the java.util.Collections.singleton, java.util.Collections.singletonList, and java.util.Collections.singletonMap methods added in Java 1.3 or this GitHub discussion titled Singleton list types (single item list), or this Python tutorial: "A list with a single object is sometimes referred to as a singleton list".

The Software Design Pattern that ensures that a class can only be instantiated once is called the Singleton Design Pattern, [a type that can only be inhabited by one value is called a singleton type.

So, a good name for your method would be is_singleton_list, or, since it is an instance method of the collection, just is_singleton.

Jörg W Mittag
  • 103,514
  • 6
    To be clear, is a "singleton list" one that can only contain one item, or is it one that does contain just one for the time being, whilst being capable of containing any number? – Steve Aug 25 '20 at 15:44
  • 7
    Since I am a fan of functional programming, there is no difference to me, since a list is immutable and only ever contains what it contains right now :-D So, a singleton list is a list that contains one element, period. However, for the case of a mutable list, I would argue that it depends on how many elements the list contains, not how many it could contain. (A list that can only contain at most one item wold be isomorphic to Option, a list that can only contain exactly one item is isomorphic to the item.) – Jörg W Mittag Aug 25 '20 at 15:49
  • In all fairness, and even if not totally convinced that it might not create some confusion, this is a well argumented answer ! – Christophe Aug 25 '20 at 16:18
  • 1
  • 6
    With all due respect, the term they are looking for is surely not singleton. At least ever since 1994, the term singleton in software engineering refers to a "Highlander" design pattern: "There can be only one." This does not mean that tonight he is the only man at the bar; it means there can be only one overall, period. This is not mathematics; this is Sparta. – Peter - Reinstate Monica Aug 26 '20 at 08:55
  • @JörgWMittag, the problem with that conception - of making a one-item-only list equivalent to the item itself - is that it fails to acknowledge the presence of the containing structure. To again adapt an analogy I just used in another comment, if I ask you to pass me the bowling ball, it does matter whether it's in the bowling ball bag or not - I'm not going to throw it down the lane inside the bag, "because they are isomorphic". A rule which says all bags must contain a ball (except during assembly and disassembly), doesn't make the bag and the ball the same thing, or indivisible. – Steve Aug 26 '20 at 12:45
  • @Peter-ReinstateMonica Terms (in compute science and elsewhere) are overloaded — that’s just a fact, deal with it. “Singleton” is a widespread name for exactly this, and there’s no implication of “highlander” design pattern in this. Using this term here is fairly well established and there’s absolutely no confusion with the singleton design pattern. What’s more, the so-named design pattern is widely discouraged anyway, so using the word to refer to the design pattern isn’t really that common any more. In my domain (not Java), it vastly more often refers to singleton collections. – Konrad Rudolph Sep 23 '20 at 09:40
  • @KonradRudolph Personal experiences obviously differ, but why don't you google "singleton computer science" and report back what you find on the first page, just to humor me? – Peter - Reinstate Monica Sep 23 '20 at 10:25
  • @KonradRudolph And now imagine my Romanian colleagues to who management outsourced the validation tests. They are doing a decent job but just aren't native speakers. They see something in the code named "singleton", a word they are unsure about. I know what they'll do as well as you do, and what that will tell them. – Peter - Reinstate Monica Sep 23 '20 at 10:30
  • @Peter-ReinstateMonica Nobody is disputing the relative prevalence of the terms in general (but interestingly one of the top hits is the Wikipedia article which lists several meanings of the term in computer science). — I am however saying that much of this is historical (the singleton pattern used to be incredibly fashionable), and that this doesn’t represent the current prevalence. As for your Romanian colleague, I’ll commiserate (I’m not a native speaker myself), but obviously he knows as well as I that English proficiency is important. – Konrad Rudolph Sep 23 '20 at 10:31
  • @KonradRudolph Are you saying you were looking at google through the way back machine? ;-) Also, none of the various "computing" uses listed on the wikipedia article represent this use! – Peter - Reinstate Monica Sep 23 '20 at 10:32
  • @Peter-ReinstateMonica No, I am saying that Google obviously doesn’t only show documents published within the last year. I am now also saying that you obviously know this and that your argument is intentionally disingenuous. – Konrad Rudolph Sep 23 '20 at 10:33
  • @KonradRudolph Don't know about disingenuous but it seems obvious that google hits give a first approximation of current usage. You may not like it, but since Design Patterns the word is linked to that, and my argument is that that hasn't changed much ever since. You'd have to come up with substantially more evidence than "In my domain" (which I take the liberty to understand as "what I have seen" until you come up with a link collection in your domain). – Peter - Reinstate Monica Sep 23 '20 at 10:41
  • @Peter-ReinstateMonica: In my answer, I have given examples from JavaScript, Java, Python, Scala, and multiple examples from maths, and a dictionary definition, all of which refer to a different concept than the Singleton Design Pattern. In the Ruby Programming Language, there are also multiple concepts called "singleton" which have nothing to do with the Singleton Design Pattern. Here is an example from the C# community, using the term "singleton" to refer to a collection with 1 element. I feel that between Java, JavaScript, C#, Python, and Ruby … – Jörg W Mittag Sep 23 '20 at 11:49
  • … there seems to be a non-negligible amount of communities where the term singleton is used to refer to something other than the Singleton Design Pattern. – Jörg W Mittag Sep 23 '20 at 11:50
  • I actually didn't see that we were discussing under your answer -- I thought it was under mine. I would have been a little less forceful in my replies. I'll take a closer look at your examples. – Peter - Reinstate Monica Sep 23 '20 at 12:50
  • Well, I'm still not convinced. The Singleton containers in Java are immutable; that makes a difference to me ("there can be only one" holds); we had this discussion elsewhere. And in the context of computing, your dictionary entry actually explicitly and exclusively mentions the design pattern; I feel it is not a convincing argument. That said, of course there are all kinds of uses of the word, and yes, the original meaning is something like "a separate thing", "one of a kind" or so. More general uses are not wrong; my argument is that they have the potential to mislead. – Peter - Reinstate Monica Sep 23 '20 at 12:53
3

There is no standard terminology for this. Even 'plural container' doesn't see any use; if I'd encounter that term in the wild, I would assume it's a group of plural words (["tests", "programs"]). Therefore, even if you would find a fitting term (e.g. 'trivial'), you should not use it to name methods if you want other people to understand from the name alone what it does.

The current name is reasonably clear, but perhaps you need a more generic method. I'm not sure about Python naming conventions, but I'm used to start methods with a verb:

def check_length(self, x, y, length):
    return len(x) == length and len(y) == length
Glorfindel
  • 3,137
  • Upvote for the first half alone! Glorfindel would assume a "plural container" is collection of plural items, while I'd assume it's some kind of special container that stores multiple sets of items. It's definitely not clear what that would mean, possibly because it's so redundant as to be confusing. I'm relieved to see that this question is the highest result when googling "plural container" programming – A C Sep 25 '20 at 00:42
3

Plural container is wrong to begin with. There is nothing plural about the container, it will remain to be one container no matter how many elements it contains.

Your method could be named GetNaiveCount(). The result type could be an enum NaiveCount: None, One, Many. I would make it a property NaiveCount though if the language supports that.

Martin Maat
  • 18,435
2

in set theory this can be called a singleton or a unit set, see also https://en.wikipedia.org/wiki/Singleton_(mathematics)

Benni
  • 896
  • 2
  • 7
  • 15
1

From a practical programmer's point of view, it is just a list. It has the interface and the behaviour of a list. It's type is list. The fact that it currently holds only one element doesn't change that, and that state may change soon anyhow.

Personally I will instantly understand if you tell me you have "a list of one (element)", but I will ask you to clarify if you use "singleton list" or one of the other special terms presented here, because most of them are already (over)loaded with a certain meaning. It may make sense within a special domain to define a term for it, to differentiate between several specializations that need to be treated differently and make communication less ambiguous, but then your peers will hopefully understand the subtleties (if not it's your issue to introduce it to them).

But don't make things more complex than they need to be.

Murphy
  • 821
-1

In mathematics, and more precisely in the set theory, a set with only one element is called a singleton. Since lists are sets, then a list with one element can be called a singleton list.

Yes, lists are sets... or at least they can be encoded in set theory:

s is a list of type X iff there exists n in Nat such that s:[1,n] --> X

Then, s is a function and so is a set of ordered pairs. In this case the ordered pairs in s are of the form (k,x) where k in [1,n] and x in X.

In this way, the list [hello,world] is encoded as the set {(1,hellow),(2,world)}. And the singleton list [hello] becomes the singleton set {(1,hello)}. Clearly, the empty list [] is the empty set {}.

The first time I saw this encoding was in the Z formal notation (for formal specification) designed by Jean-Raymond Abrial.

  • what do these dollar signs mean? $X$, $s$ etc – gnat Sep 23 '20 at 14:49
  • I tried to write math formulas in Latex which is supposed to be supported here, but for some reason it doesn't. I gave up after 45'. It should look like here. – Maximiliano Cristiá Sep 24 '20 at 19:16
  • 1
    @MaximilianoCristiá Only some sites have mathjax enabled due to overhead. – Deduplicator Sep 25 '20 at 15:30
  • @Deduplicator Thanks! I didn't know that. Changed to plain text. – Maximiliano Cristiá Sep 27 '20 at 01:46
  • Yes, lists are (quite obviously) sets, but with a caveat. In Mathematics, a set is a group of distinct items defined "for good". E.g. the set {x is a natural number, x < 10 ^ x > 8} is eternally specified to contain 1 element. If you add one by moving the boundaries, you have a different set. That's why it makes sense to give such a set a specific name, singleton. Not so with (normal) lists in programming: You can trivially add an element to the same list! The list does not change identity; the set does. (And yes, if you can't add an element it makes sense to special-name the list!) – Peter - Reinstate Monica Sep 28 '20 at 09:51
  • 1
    What is the list, its name or its contents? {x is a natural number, x < 10 ^ x > 8} is an unnamed set, you are just describing its contents. Semantically if you add an element to a list, it's another list; it can't be the same list because in that case your program hasn't moved from one state to another, when it did. – Maximiliano Cristiá Sep 29 '20 at 12:43
-2

The distinction is only meaningful in languages with Run-Time Type Information, or can have types implicitly contain other types, such as any or Object. At runtime in a function whose type is already decided, a 1-tuple of an int and an int don't have type tags, the set of operations applicable to each is identical.

A list is usually a container of dynamic length, so its size is only meaningful if you use dependent typing or contract programming, because the length is dynamic and not truly a property of the type in the mathematical sense.

The tuple syntax (1, 2) in Haskell is of type (Int, Int)

(1) is the same thing (Int) which is the same type as Int. In other words, the type of a 1-tuple of a type is the same type as that type. If you think about it, this makes sense in a language where variables can only be of one type: The same set of operations apply to a 1-tuple of a thing as to a thing itself, because the type is predetermined outside the function call.

  • 1
    I'm not the downvoter, but your argument doesn't strictly make sense. A 1-tuple is not necessarily the same as a scalar - at least, the equivalence cannot be sustained if the language permits multi-dimensional arrays, where an array does not just have a length, but also a rank, and a length-1-rank-1 array (a tuple) is different from a length-1-rank-0 array (a scalar). – Steve Aug 26 '20 at 16:41
  • @Steve Rank and length of a tuple is a type-level construct. In this context, dynamically typed means any language with RTTI. For an instantiated function, the types have already been decided what to run. When types are removed, a length-1 rank-0 tuple is the same as a length-1 tuple and a scalar. – noɥʇʎԀʎzɐɹƆ Aug 27 '20 at 20:09
  • A length-1-rank-0 array is a scalar. But that's not the same structurally as a length-1-rank-1 array. The value-carrying capacity may be identical, but the mechanics of the storage are not identical. And if the storage is not identical, the operations cannot possibly be identical. It's the introduction of rank (i.e. nesting level, or multiple axes) which forces the distinction, whereas the distinction does not exist when only length is considered (i.e. in a flat model with only sequences of values). It's a crucial oversight of most mathematical treatments as applied to computing. – Steve Aug 28 '20 at 00:31
  • @Steve I don't see what you're saying. If the length and rank are part of the type, and the value-carrying capacity is part of the type, and the type has already been resolved prior to the function call, then clearly the distinction is meaningless. (Otherwise the function effectively takes a Either<Length 1 rank 0, Length 1 rank 1, int>. Either is the same as Maybe.) – noɥʇʎԀʎzɐɹƆ Aug 28 '20 at 17:53
  • As I say, the value-carrying capacity (of a scalar type vs a length-1-rank-1 array) may be the same, but the storage of the value is structured differently. The length-1-rank-1 array contains a scalar. They cannot possibly be the same type, in any type system that admits nested arrays. There may be an implicit conversion to and from a scalar, but that only confirms the existence of the type distinction in principle. The distinction also exists in natural language and ordinary understandings - a bowling ball bag containing a bowling ball, is not the same item as just the bowling ball. – Steve Aug 28 '20 at 18:50
  • Another way of emphasising the point is that the type is not just a description of the values carried, but also the way those values are structured (either actually or conceptually) in storage. If you take the functional interpretation of tuples, then a scalar may have a type int, but a length-1-rank-1 array (a 1-tuple) will have a type int -> int (it takes an index number, also coincidentally an int, and which in this case can only be 1, and returns the scalar value, which is the int). Their types are not equivalent notwithstanding that only one value is actually carried in both cases. – Steve Aug 28 '20 at 18:56
-2

If it can hold zero or one element, a common name is optional, for example C++ has std::optional.

Simon Richter
  • 1,568
  • 9
  • 10