Hold

Hold

Hold[ expr ] keeps an expression unexecuted, so that the commands in it are executed later.

Usages

Hold[ expr ]

Hold[ expr ] keeps expr as it is: commands inside it are not executed. Script variables inside expr are replaced by their values. Hold is mainly used to pass a mapping with commands to another command, such as Map or Select, which then executes the commands for each element.

Example in out
The command inside Hold is not executed. Hold[ CalcPlus[1, 2] ] Hold[CalcPlus[1, 2]]
Script variables are replaced by their values. a := 3
r := Hold[a + 1]
r = Hold[3+1]
A mapping with a command, used in Map: RandomInt is executed for each element. f := Hold[x -> RandomInt[{1, x}]]
r := Map[f, {5, 10}]
for example r = {2, 4}
A condition for Select. Select[ Hold[x -> Type[x] == Variable], 2x + 3y ] {x, y}
Details
  • Simplify does not remove Hold: Simplify[ Hold[CalcPlus[1, 2]] ] stays Hold[CalcPlus[1, 2]].
  • Do not show an expression with Hold to students: it is displayed with the word "Hold", as Hold(x+1)\text{Hold}\left(x+1\right).

See also: Map, Select, CreateMapping