Message Boards Message Boards

0
|
7875 Views
|
10 Replies
|
6 Total Likes
View groups...
Share
Share this post:

What is going on with Names[ ] ??

POSTED BY: David Vasholz
10 Replies

Good point. Perhaps there is a proof that "booby traps" cannot be completely avoided!

POSTED BY: David Vasholz

It seems to me that it would be better if symbols never evaluated unless at least empty square brackets are present

How would you define variables then? x=1.

POSTED BY: Szabolcs Horvát

x=1 is Set[x,1] but others like Today, Now and Here evaluate without brackets.

POSTED BY: Gustavo Delfino
Posted 5 years ago

It appears that Today, Now and Here are really "user friendly" shells of DateObject and GeoPosition.Where the required, bracketed, argument is automatically supplied according to the users time and location.

In[1]:= InputForm[Today]
Out[1]//InputForm=DateObject[{2019, 5, 8}, "Day", "Gregorian", 2.]

In[2]:= InputForm[Now]
Out[2]//InputForm=DateObject[{2019, 5, 8, 19, 56, 50.1329091`9.452697759260092}, "Instant", "Gregorian", 2.]

In[4]:= InputForm[Here]
Out[4]//InputForm=GeoPosition[{63.83, 20.26}]
POSTED BY: Hans Milton
Posted 5 years ago

Or, one can say that an inbuilt symbol that do not change its evaluated output if its given an argument, in square brackets, can be considered to be a constant. Like Red:

In[1]:= Red
Out[1]= RGBColor[1, 0, 0]

In[2]:= Red[7]
Out[2]= (RGBColor[1, 0, 0])[7]

Supplying an argument does nothing.

In the case of Now it obviously does not have a constant value, seen over time. But the value it has is not affected by WL evaluation. Instead the value is continually updated and stored on your PC. So WL just returns the currently stored value at evaluation.

POSTED BY: Hans Milton

Thank you for your comment. It seems to me that it would be better if symbols never evaluated unless at least empty square brackets are present, as is the case for such venerable examples as Sin, Cos, or even Names. More generally, it would be it would be good if Mathematica did a better job of getting rid of "bobby traps." That said, the overall quality is suberb.

.

POSTED BY: David Vasholz

As a side note to all readers: please avoid evaluating ToExpression/@Names[ ]. This is dangerous. It will effectively evaluate the values of every single (internal or public!) symbol that the kernel knows about. There's no telling what that'd do in a given Mathematica version ...

POSTED BY: Szabolcs Horvát

Even in Version I2 I feel that it would make more sense for all the heads revealed by Tally to be Symbol

What would you expect from the following?

foo := Echo["Hello!"]
ToExpression["foo"]

The behaviour has not changed since early versions. If a symbol has an assigned value, it will evaluate. That evaluation can have arbitrary side effects.

To work around this, we need the 3rd argument of ToExpression, the HoldAll attribute, and Unevaluated.

In[11]:= SetAttributes[head, HoldAll]
head[sym_] := Head@Unevaluated[sym]
ToExpression[#, InputForm, head] & /@ Names["System`*"] // Counts

Out[13]= <|Symbol -> 6556|>
POSTED BY: Szabolcs Horvát
POSTED BY: David Vasholz

You could try, instead:

Tally[Head /@ ToExpression /@ Names["System`*"]]

With Mathematica 12.0, the result I get is:

{{Symbol, 6331}, {GrayLevel, 5}, {RGBColor, 20}, {DirectedInfinity, 
  4}, {Dashing, 3}, {GeoPosition, 2}, {Complex, 1}, {DateObject, 
  5}, {String, 74}, {Thickness, 2}, {List, 55}, {Integer, 
  27}, {CloudObject, 4}, {FrontEndObject, 1}, {Entity, 
  3}, {Association, 4}, {Function, 1}, {Real, 10}, {LinkObject, 
  1}, {PersistenceLocation, 1}, {Rational, 1}}
POSTED BY: Murray Eisenberg
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract