# Are Strings Scalar?

I have always considered strings scalar and the main reason for that is that in programming we treat string values a lot like primitive values. But this and this Wikipedia definitions, as far as I understand them, speak against the scalar nature of strings.

In programming scalar types are often seen as the opposite of object types and strings are usually counted as scalar, because they, as well as primitive types, have natural ordering, and thus can be compared and tested for equality by means of the language itself, as opposed to object types, which would usually require custom logic for those operations.

By scalar here I mean firstly the opposite of object types, but I want to know whether being scalar is a real property of strings or are we just calling them scalar in programming because their behavior resembles primitive types

This leaves me with the question: are strings of text scalar values from the point of view of theoretical computer science?

• What's the context in which this came up? What definition of scalar are you using? Why do you want to know whether it's a scalar or not -- how will you use the answer? – D.W. Sep 16 '15 at 15:49
• What exactly do you mean by "scalar" here? Why do you think TCS would talk about the same thing as programming languages when they say "string"? – Raphael Sep 16 '15 at 15:52
• @D.W. In programming scalar types are often seen as the opposite of object types and strings are usually counted as scalar, because they, as well as primitive types, have natural ordering, and thus can be compared and tested for equality by means of the language itself, as opposed to object types, which would usually require custom logic for those operations. The reason I want to know whether strings are scalar is curiosity - I have always thought that strings do not exactly belong with the primitive types. – Robert Mugattarov Sep 16 '15 at 16:21
• @RobertMugattarov, OK: I suggest that you edit the question to provide that context & clarification. That will definitely help attract better answers. Please don't just leave clarifications in the comments; edit the question. Questions should stand on their own -- people shouldn't have to read the comments to understand what you're asking. (However, I'm not sure what question that leaves... it sounds like you've answered your own question.) – D.W. Sep 16 '15 at 16:24
• Doesn't this depend entirely on the programming language being used? In C, for example, there are no objects, so I guess everything's a scalar; in Java, strings are objects. – David Richerby Sep 16 '15 at 19:32

I'm used to hearing the word "scalar" used in physics and linear algebra and such, not theoretical computer science. In that context, we're interested in distinguish between a scalar vs a vector: e.g., an element of $\mathbb{R}$ vs an element of $\mathbb{R}^d$. The word was invented to help us talk about that distinction.

However, that distinction doesn't seem particularly relevant here.

I haven't heard the word "scalar" used much in theoretical computer science.

To the extent that we invented a meaning for "scalar" in theoretical computer science, I'd probably reserve it for values that can be stored in $O(1)$ space, and where basic operations on those values can be performed in $O(1)$ time. For instance, an int would qualify under this. If that's the definition you have in mind, a string doesn't qualify, as it is a constant-space item and operations on it can't be done in constant time.

• Regarding your last paragraph, that would only work out in the unit-cost model. Also, how do integers and strings really differ? $\mathbb{Z}$ and $\Sigma^*$ are isomorphic, after all (as long as $\Sigma$ is finite). – Raphael Sep 16 '15 at 16:13
• @Raphael, Yup. I was thinking of the RAM model -- or just measuring how long a real processor takes. By this definition an int would be a scalar (because it is fixed width; e.g. 32 bits or 64 bits wide), but an integer wouldn't be a scalar (because it is unbounded). However, I haven't heard the word "scalar" used much in computer science, and when I have heard it used, I haven't heard it be defined formally, so I don't know if this is n accepted definition -- this is just how I'd use it, if someone pointed a gun at me and forced me to use the word. – D.W. Sep 16 '15 at 16:16
• I'd (try to) take away their gun, but fair enough. ;) – Raphael Sep 17 '15 at 9:08

So, I think scalar and object aren't particularly well defined here. What I think of it as is that scalar is a primitive type, that can't be broken down, and an object type is a composite type: it is built up from one or more other types.

A string is not inherently scalar, and how they are treated depends much on your choice of programming language.

On the one end, we have Haskell, where String is literally a synonym for [Char]: that is, a string is just a list of characters. In this sense, Char is a primitive type, and String is most definitely NOT scalar.

C is similar, where a string is just a pointer to an array of char, and C++ string objects are clearly not scalar since they are, well, objects.

However, some languages, like Java and JavaScript, distinguish strings and lists of characters. However, the main reason for doing this is efficiency. They make string its own primitive type, and do some weird stuff internally with interning, so that (in some cases), a string is only stored in one place in memory, and comparing them is as easy as checking pointer equality.

I would argue that this is more of an implementation detail more than anything. Usually, there is some sort of isomorphism between String and a sequence of Char, and there are non-scalar-style operations, like getting the nth character of a string. If you can do that, in some way your string is linked to a collection of characters, making it non-scalar.

Likewise, the fact that you can express string literals doesn't necessarily make them scalar. In Haskell, "Foo" is just syntactic sugar for 'F':'o':'o':[] i.e. the linked list of characters.