Message Boards Message Boards

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

What is going on with Names[ ] ??

I became a "convert" to Mathematica way back in 1992, but have not used it much recently. At one time I could begin to grasp the overall scope of Mathematica by invoking Names[ ], which gives a large list of all the recognized strings. In particular an invocation of Tally[ Head/@ToExpression/@Names[ ]] should verify that essentially all the String elements have been converted into Symbol elements. Instead I get a bunch of error messages and other weird stuff that makes me think that the internet has not been very well integrated into Mathematica. My disturbing results were obtained from version 10.4.

Any comments would be appreciated. Thank you.

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

Thank you for your suggestion of using a more restricted command. Shown below is what I get from my Version 10.4 , which indicates that there is some improvement in later versions. Even in Version I2 I feel that it would make more sense for all the heads revealed by Tally to be Symbol, and for no symbol (or at least an absolute minimum of symbols) to be activated without at least the presence of square brackets..

In[1]:= Tally[Head /@ ToExpression /@ Names["System`"]] During evaluation of In[1]:= $IterationLimit::itlim: Iteration limit of 4096 exceeded. >> During evaluation of In[1]:= CloudAccountData::notauth: Unable to authenticate with Wolfram Cloud server. Please try authenticating again. >> During evaluation of In[1]:= CloudObjectNameFormat::una: Unable to apply the specified name format UserURLBase. During evaluation of In[1]:= CloudObject::unauth: URI /CloudExpression only valid when authenticated. >> During evaluation of In[1]:= CloudObjectNameFormat::una: Unable to apply the specified name format UserURLBase. During evaluation of In[1]:= CloudObject::unauth: URI /CloudSymbol only valid when authenticated. >> During evaluation of In[1]:= CloudObjectNameFormat::una: Unable to apply the specified name format UserURLBase. During evaluation of In[1]:= General::stop: Further output of CloudObjectNameFormat::una will be suppressed during this calculation. >> During evaluation of In[1]:= CloudObject::unauth: URI /PermissionsGroup only valid when authenticated. >> During evaluation of In[1]:= General::stop: Further output of CloudObject::unauth will be suppressed during this calculation. >> Out[1]= {{Symbol, 5547}, {Hold, 1}, {GrayLevel, 5}, {RGBColor, 20}, {DirectedInfinity, 4}, {Dashing, 3}, {GeoPosition, 2}, {Complex, 1}, {DateObject, 5}, {String, 61}, {Thickness, 2}, {List, 45}, {Integer, 24}, {FrontEndObject, 1}, {Entity, 3}, {Association, 1}, {Function, 1}, {Real, 10}, {LinkObject, 1}, {Rational, 1}}*

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