Since everything is an expression in Mathematica, why must a string object be formed by "abc"
but not by a String[abc]
expression?
You can look at a string's head by:
Head["abc"]
String
But you can not produce the same string by String
String[abc]
which, from my point of view, seems inconsistent with the principle that Everything Is an Expression.
However, I noticed that the basic Symbol
object, on the other hand, can be formed by something like Symbol["a"]
.
The same question goes for four number objects (Integer
, Real
, Rational
, and Complex
). You can't say an integer 1 by something like Integer[1]
, can you?
Edit:Rational
and Complex
can be produced by their respective heads. So The question is valid only for String
and two number objects, i.e. Integer
and Real
.
Answer
String
and Integer
are what I termed "implicit heads" while writing:
Rather than being part of the standard expression itself, at least as I understand it, these implicit heads instead serve the purpose of providing a "type" for pattern matching. (With a pattern _String
, _Integer
, etc.) The atomic expressions themselves are stored in a low-level format and handled transparently behind the scenes.
Of the heads you list Rational
and Complex
are exceptions as these are a kind of hybrid head: you can use them to enter data:
{Complex[1, 2], Rational[5, 8]}
{1 + 2 I, 5/8}
Critically you can also match patterns within these heads:
{1 + 2 I, 5/8} /. {Complex[a_, b_] :> foo[a, b], Rational[n_, m_] :> bar[n, m]}
{foo[1, 2], bar[5, 8]}
Nevertheless these expressions are considered "atomic" and they cannot be manipulated other ways that apply to standard expressions:
AtomQ /@ {1 + 2 I, 5/8}
foo @@@ {1 + 2 I, 5/8}
{True, True}
{1 + 2 I, 5/8}
Comments
Post a Comment