This discussion came up in a previous question and I’m interested in knowing the difference between the two. Illustration with an example would be nice.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Basic Example
Here is an example from Leonid Shifrin’s book Mathematica programming: an advanced introduction
It is an excellent resource for this kind of question. See: (1) (2)
Table[a, {5}]{4, 4, 4, 4, 4}{10, 5, 2, 1, 3}Complicated Example
The example above may give the impression that once a definition for a symbol is created using
Set, its value is fixed, and does not change. This is not so.f = ...assigns tofan expression as it evaluates at the time of assignment. If symbols remain in that evaluated expression, and later their values change, so does the apparent value off.It is useful to keep in mind how the rules are stored internally. For symbols assigned a value as
symbol = expression, the rules are stored inOwnValues. Usually (but not always),OwnValuescontains just one rule. In this particular case,The important part for us now is the r.h.s., which contains
xas a symbol. What really matters for evaluation is this form – the way the rules are stored internally. As long asxdid not have a value at the moment of assignment, bothSetandSetDelayedproduce (create) the same rule above in the global rule base, and that is all that matters. They are, therefore, equivalent in this context.The end result is a symbol
fthat has a function-like behavior, since its computed value depends on the current value ofx. This is not a true function however, since it does not have any parameters, and triggers only changes of the symbolx. Generally, the use of such constructs should be discouraged, since implicit dependencies on global symbols (variables) are just as bad in Mathematica as they are in other languages – they make the code harder to understand and bugs subtler and easier to overlook. Somewhat related discussion can be found here.Set used for functions
Setcan be used for functions, and sometimes it needs to be. Let me give you an example. Here Mathematica symbolically solves the Sum, and then assigns that to aF(x), which is then used for the plot.If on the other hand you try to use
SetDelayedthen you pass each value to be plotted to theSumfunction. Not only will this be much slower, but at least on Mathematica 7, it fails entirely.If one wants to make sure that possible global values for formal parameters (
xhere) do not interfere and are ignored during the process of defining a new function, an alternative toClearis to wrapBlockaround the definition:A look at the function’s definition confirms that we get what we wanted: