MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: set versus set-delay

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75245] Re: set versus set-delay
  • From: dh <dh at metrohm.ch>
  • Date: Sat, 21 Apr 2007 23:16:56 -0400 (EDT)
  • References: <f09h44$19r$1@smc.vnet.net>


Hi,

this is a bit oversimplified. If a parameter (not an argument!) is used, 

with SET, the value of the argument at the time of definition is used. 

On the other hand, with SETDELAYED, the value of the argument at the 

time of the function call is used.

hope this helps, Daniel



siewsk at bp.com wrote:

> Informative Article

> ===================

> 

> SET versus SETDELAY

> 

> 

> Newbie are always told to use SETDELAY instead of SET when they create

> a mathematica function.

> 

> The objective of this article is to explain the differnce between SET

> 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: set versus set-delay
  • Next by Date: Re: ContourPlot3D Problem
  • Previous by thread: set versus set-delay
  • Next by thread: Re: set versus set-delay