Skip to main content
24 votes

How to pass variables by reference and by value

You asked for a general explanation instead of just focusing on specific application examples, so here it goes ... The concepts of "pass by reference" and "pass by value" that you ...
Szabolcs's user avatar
  • 236k
19 votes

How do you set attributes on SubValues?

This is an old discussion but is about an issue that resurfaces every now and then. One of the best (for a given sense of elegance) answers is the one posted on Stack Overflow ...
Batracos's user avatar
  • 2,105
17 votes
Accepted

Duplicate the swap function

Attributes[swap] = HoldAll; swap[x_, y_] := {x, y} = {y, x}; x = 1; y = 2; swap[x, y]; {x, y} {2, 1} There are two ...
Mr.Wizard's user avatar
  • 272k
14 votes

Part not behaving the way I expect it to

The way Set works in setting parts of an expression is this: symb[[..<part specification>..]] = values; The component <...
Michael E2's user avatar
  • 239k
14 votes
Accepted

How would you explain Nothing[] weirdness?

I think the best way to understand this behavior is with this example, ...
Jason B.'s user avatar
  • 69.2k
11 votes

Problem with function inside brackets. Bug?

No, not a bug. Let's think about how AppendTo may be implemented (even though the actual implementation isn't inspectable). ...
Szabolcs's user avatar
  • 236k
11 votes
Accepted

Confusion about HoldFirst attribute of Set

The problem is not related to Set in general but to how Set works with Part on a left hand ...
Kuba's user avatar
  • 137k
11 votes

Can't understand one paragraph in Power Programming with Mathematica

He is simply saying that the first argument of Set is not evaluated before Set creates the definition. However, the sub-parts ...
Szabolcs's user avatar
  • 236k
10 votes
Accepted

Problem with function inside brackets. Bug?

Here's the issue. In the second (non-working) code, RandomInteger[{1, Length[a]}] is evaluated twice, as we can see by Trace...
march's user avatar
  • 24k
10 votes

Confusion about HoldFirst attribute of Set

Kuba's answer explains why the first form does not work, but it does not address why the second form does work. That has been discussed in other questions DownValue assignment using Apply Why is the ...
Mr.Wizard's user avatar
  • 272k
10 votes
Accepted

Error Message when nothing should be evaluated

This is due to the special behavior of SetDelayed (:=) with regards to the first argument (see e.g. this question): The ...
Lukas Lang's user avatar
  • 34.2k
9 votes

What is the difference between Set and SetDelayed?

Another quick example: I have a function: f[x1_,x2_]:= x1+x1*x2 and I want to find it's derivative according to x2 at specific locations of x1 and x2. ...
henry's user avatar
  • 2,510
9 votes
Accepted

Apply ToString to a list of variables

In addition to Mr.Wizard's answer here is a collection of other possibilities: ...
Alexey Popkov's user avatar
9 votes
Accepted

What is the point of tagset /:?

TagSet is useful for assigning UpValues to symbols. These are often used to make built-in functions do something different for ...
Sjoerd Smit's user avatar
9 votes
Accepted

Why Optional in Set is not held by the HoldFirst attribute?

From the "Details" of the docs for HoldPattern The left-hand sides of rules are usually evaluated, as are parts of the left-hand sides of assignments. You can ...
Michael E2's user avatar
  • 239k
9 votes
Accepted

How can I list the common elements of two lists?

If you need the duplicate 9 in the output: ...
qbg's user avatar
  • 176
8 votes

Assign the results from a Solve to variable(s)

Here's a painless solution: s = Solve[y^2 == 13 x + 17 && y == 193 x + 29, {x, y}]; Assign the four results of the solution s above to a variable each, ...
Vixillator's user avatar
8 votes

How can I assign many values each to a different variable?

First of all, please note that coding this way in Mathematica is asking for trouble. Use of (lots of) mutable variables is strongly discouraged. The following will show you why: Clearing all symbols <...
Sascha's user avatar
  • 8,499
8 votes

How to pass variables by reference and by value

You need: SetAttributes[myfunc, HoldAll] You then have: ...
user21's user avatar
  • 40.1k
8 votes

assignments within Manipulate

Updated to handle $ showing from using module variable. Add TrackedSymbols :> {c} and add ...
Nasser's user avatar
  • 147k
8 votes

Transferring DownValues

You can make use of the undocumented Language`ExtendedDefinition function that underlies the Wolfram Cloud. Suppose: ...
Carl Woll's user avatar
  • 131k
7 votes
Accepted

Assigning values to association in DownValues

This is nothing unique to Association; the same issue affects simple lists: a[1] = {1, 2, 3}; a[1][[2]] = 7; Set::setps: a[...
Mr.Wizard's user avatar
  • 272k
7 votes

Duplicate the swap function

Just a reminder for advanced users, to show the complexity of the problem : keywords : David Wagner power programming swap byname
andre314's user avatar
  • 18.7k
7 votes

Can't understand one paragraph in Power Programming with Mathematica

If Set would not have the HoldFirst attribute, it would lead to undesired behavior if variables on the left hand side have ...
Henrik Schumacher's user avatar
7 votes
Accepted

Transferring DownValues

DownValues[cc] = DownValues[cctemp] /. cctemp -> cc; cc @@@ {{1, 2}, {2, 6}, {3, 7}, {1, 9}} {13, 8, 4, 87} If cc has ...
kglr's user avatar
  • 399k
7 votes

What is the point of tagset /:?

As an addendum to Sjoerd's great answer, I would simply like to show a use case for defining a custom data type and then using TagSet to create what is called an ...
gwr's user avatar
  • 13.5k
7 votes

Why the difference in performance? Array vs List and `Delete` vs `Nothing`

data2[[3]] = x; // AbsoluteTiming might give a hint. I explain it to myself as follows: At that point when you execute ...
Henrik Schumacher's user avatar
7 votes
Accepted

Cannot assign variable value inside Do Loop

Mathematica is a term rewriting system. It will replace x1, x2, x3 by their values as soon ...
user3257842's user avatar
7 votes
Accepted

Defining a function iteratively without Mathematica getting stuck in a recursion loop

You can define the function(s) recursively: ...
bill s's user avatar
  • 69.4k
7 votes

Error message not reproduced at reevaluation, SetDelayed cache?

We can cause the message to appear every time by calling Update[] in the body of the definition: ...
WReach's user avatar
  • 69.1k

Only top scored, non community-wiki answers of a minimum length are eligible