patternMinor
Are Strings Scalar?
Viewed 0 times
arescalarstrings
Problem
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?
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?
Solution
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
C is similar, where a string is just a pointer to an array of
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,
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.Context
StackExchange Computer Science Q#47237, answer score: 5
Revisions (0)
No revisions yet.