SetDelayed

lhs:=rhs

assigns rhs to be the delayed value of lhs. rhs is maintained in an unevaluated form. When lhs appears, it is replaced by rhs, evaluated afresh each time.

Details

  • SetDelayed has attribute HoldAll, rather than HoldFirst.
  • You can make assignments of the form lhs:=rhs/;test, where test gives conditions for the applicability of each transformation rule. You can make several assignments with the same lhs but different forms of test.
  • lhs:=rhs returns Null if the assignment specified can be performed, and returns $Failed otherwise.

Examples

open allclose all

Basic Examples  (1)

Scope  (9)

Left Hand Sides  (4)

A variable defined with SetDelayed is evaluated every time it is used:

Make definitions for special and general cases using immediate and delayed assignments:

Make conditional definitions:

Define a function by several conditional cases:

Different Kinds of Values  (5)

Ownvalues:

Downvalues:

Subvalues:

Upvalues:

Numerical values:

Applications  (3)

Define a procedure that computes a square root with Newton's method:

Perform a calculation on demand and cache the result:

Definitions for unevaluated expressions can implement call-by-name semantics:

The global variable has been modified:

Properties & Relations  (9)

The right side of an immediate definition is evaluated when the definition is made:

The right side of a delayed definition is evaluated each time the definition is used:

The arguments of the left side of a definition are evaluated before the definition is made:

Definitions with the same left side overwrite earlier ones:

The pattern variable is renamed if necessary inside a nested scope:

Delayed assignment introduces a scope that is not affected by global variables:

Immediate assignment does not introduce a scope:

Use a rule to do a transformation to a particular expression:

Use a definition to do a transformation automatically for all expressions involving g:

More specific definitions are put in front of more general ones:

Definition prints definitions associated with a symbol:

Information prints various information about a symbol, including any definitions:

DownValues returns a list of rules corresponding to any downvalues defined:

Use Unset (=.) to clear definitions with a particular left-hand side:

Clear any definitions, but not attributes:

Use ClearAll to clear attributes, too:

Possible Issues  (4)

Omitting the _ defines a transformation only for a literal value, rather than a function:

Using delayed definitions may have unexpected consequences:

The definition actually made and its behavior:

Use Evaluate to force evaluation of the right-hand side:

Or use Set to force evaluation of the right-hand side:

An iterative runaway definition:

By providing a base case, the iteration terminates:

A recursive runaway definition:

By providing a base case, the recursion terminates:

Neat Examples  (1)

Dynamic programming for the Fibonacci sequence:

New definitions have been added during the calculation:

Introduced in 1988
 (1.0)