Message Boards Message Boards

0
|
3747 Views
|
2 Replies
|
8 Total Likes
View groups...
Share
Share this post:

[?] Use of $ in the Wolfram Language?

Posted 7 years ago

Hello everyone, I found the following example (http://reference.wolfram.com/language/ref/With.html):

With allows substitution inside an unevaluated expression, preserving nested scopes:

In[1]:=With[{e = x}, Function[x, e]]
Out[1]=Function[x$,x]

Ordinary substitution does not preserve scoping:

In[2]:=Function[x, e] /. e :-> x
Out[2]=Function[x,x]

What does $ mean?

Thank you for your help.

POSTED BY: Gennaro Arguzzi
2 Replies

One might add to Kuba's answer that x and x$ are distinct symbols. The $ does not mean anything. It is added to the symbol name of x to create a different symbol. Both fortunately and unfortunately, this behavior is predictable. If you wanted to inject a function body e that has the Function variable in it, you can use x$ instead of x and count on the variable being rewritten x$.

With[{e = x$}, Function[x, e]]
(*  Function[x$, x$]  *)

On the other hand, if you wanted the symbol x$ to be distinct symbol from the Function variable, then the above code wouldn't work. You would have to use the ReplaceAll form Function[x, e] /. e -> x$. But since this rewriting happens in various contexts, better advice is the following:

Do not end the names of variables with a $.

POSTED BY: Michael Rogers

It is caused by automatic renaming performed when nested scoping constructs appear (here With and Function).

It is mentioned in documentation, read more in:

Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs

POSTED BY: Kuba Podkalicki
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