set versus set-delay

• To: mathgroup at smc.vnet.net
• Subject: [mg75208] set versus set-delay
• From: siewsk at bp.com
• Date: Fri, 20 Apr 2007 00:43:07 -0400 (EDT)

```Informative Article
===================

SET versus SETDELAY

Newbie are always told to use SETDELAY instead of SET when they create
a mathematica function.

and SETDELAY using ordinary language (what linguistics call vernacular
language)

The main differences between those two are:

SET       means    CALCULATE FIRST THEN CHANGE
SETDELAY  means    CHANGE FIRST THEN CALCULATE

To explain, consider the following two functions f ang g

f[x_] = Expand[(x+7)^2]           (* Using SET       *)
g[x_]:= Expand[(x+7)^2]          (* Using SETDELAY  *)

The two functions are identical except f uses SET while g uses
SETDELAY. In normal usage there is no difference in the two functions.
However they produce their results using different steps.

================
SET
================

Consider f[4]

(Step 1.  CALCULATE FIRST )

f[x] = Expand[(x+7)^2]

f[x] = x^2 + 14 x + 49

(Step 2. THEN CHANGE )

f[x->4] = x^2 + 14 x + 49

f[4] <- 4^2 + 14 * 4 + 49

f[4] <- 121

================
SETDELAY
================

Consider g[4]

(Step 1.  CHANGE FIRST )

g[x] := Expand[(x+7)^2]

g[x->4] := Expand[(x+7)^2]

g[4] <- Expand[(4+7)^2]

(Step 2. THEN CALCULATE )

g[4] <- Expand[(11)^2]

g[4] <- Expand[121]

g[4] <- 121

```

• Prev by Date: Re: equation solving
• Next by Date: Re: Assign delayed but fixed value
• Previous by thread: Re: equation solving in a markov model
• Next by thread: Re: set versus set-delay