Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

RE: Re: Using "=" vs ":=" [Thanks!]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70713] RE: [mg70698] Re: Using "=" vs ":=" [Thanks!]
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 24 Oct 2006 02:24:18 -0400 (EDT)

Misha,

Observe...

c = 3;
f[x_] = c + Sin[x]
3 + Sin[x]

Mathematica used the value of c that was defined in the first statement.
This might or might not be bad style depending on whether you really
intended c to be 3.

Now suppose you decide to make c a parameter, a better practice, and
write...

Clear[f]
f[c_][x_] = c + Sin[x]
3 + Sin[x]

f[d][x]
3 + Sin[x]   - d was not used.


Even though c was a pattern variable in the f definition, Mathematica
ignored that and used the defined value of c on the right hand side. (This
seems to me to be quirky behavior but nevertheless that is the way it is.
Mathematica evaluates the rhs before it gives any consideration to what the
pattern ssymbols are.) But if we use SetDelayed the definition works
properly.

f[c_][x_] := c + Sin[x]

f[d][x]
d + Sin[x]

f[c][x]
3 + Sin[x]    - because c is still defined

So if you have any symbols on the rhs of a definition that have values, then
you should use SetDelayed unless you intend those values to be used in the
definition - in which case those symbols should probably not be used as
patterns on the lhs.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


From: misha [mailto:iamisha1 at comcast.net]
To: mathgroup at smc.vnet.net

Lesson 2: Set (=) vs. SetDelayed (:=) can be important.  I'm still
trying to figure out when.  The examples in the help files and some of
the responses have helped, but I'm trying to still trying to grasp the
more general concept.

Thanks again!

Misha



  • Prev by Date: Memory problem when multiplying large sparse matrices...
  • Next by Date: Evaluate, /., {{...}}, etc.
  • Previous by thread: Re: Using "=" vs ":=" [Thanks!]
  • Next by thread: Re: Using "=" vs ":=" [Thanks!]