MathGroup Archive 2010

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

Search the Archive

Re: := vs = in some function definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113279] Re: := vs = in some function definitions
  • From: telefunkenvf14 <rgorka at gmail.com>
  • Date: Thu, 21 Oct 2010 07:04:52 -0400 (EDT)
  • References: <i9m7u7$j41$1@smc.vnet.net>

On Oct 20, 3:06 am, Sam Takoy <sam.ta... at yahoo.com> wrote:
> Hi,
>
> Have read everything I could find regarding delayed assignment, I'm
> still not quite sure when to use which.
>
> For example, in the definition
>
> f[x_] = IdentityMatrix[50].Append[Range[1, 49], x];
>
> could there ever be any reason to use :=   ?
>
> It seems that using   :=  will condemn the function to repeated ext=
ra
> work every time its called. So could there be a situation where the use
> of ":=" for the above function is recommended?
>
> Many thanks in advance,
>
> Sam

Use := (SetDelayed[]) if you want Mathematica to re-evaluate the
expression whenever you call it. SetDelayed[] is usually more
appropriate, although you will see Set[] used in dynamic programming
examples. Below is taken from the 'neat examples' in the SetDelayed[]
documentation:

Dynamic programming for the Fibonacci sequence:

In[1]:= fib[1] = fib[2] = 1;
fib[n_] := fib[n] = fib[n - 1] + fib[n - 2]

In[2]:= fib[5]
Out[2]= 5

New definitions have been added during the calculation:

In[3]:= Definition[fib]
Out[3]=

fib[1] = 1
fib[2] = 1
fib[3] = 2
fib[4] = 3
fib[5] = 5
fib[n_] := fib[n] = fib[n - 1] + fib[n - 2]

-RG


  • Prev by Date: Re: Table of tables
  • Next by Date: Re: := vs = in some function definitions
  • Previous by thread: Re: := vs = in some function definitions
  • Next by thread: Re: := vs = in some function definitions