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: [mg113260] Re: := vs = in some function definitions
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Thu, 21 Oct 2010 07:01:20 -0400 (EDT)
  • References: <i9m7u7$j41$1@smc.vnet.net>

On 20/10/10 09:06, Sam Takoy 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 extra
> 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
>

It is not always the case that letting the RHS of a function definition 
pre-evaluate (by using =) will be free of problems. Consider, for example:

f[x_] = Expand[x]

Here the Expand function will evaluate immediately, and find nothing to 
do - leaving your function f as the Identity function!

In general, defining a function with = is not usually very helpful in 
speeding up code, and you always have to look for traps such as the 
above, so I would avoid it unless you actually need pre-evaluation for 
some reason.

David Bailey
http://www.dbaileyconsultancy.co.uk


  • Prev by Date: Re: := vs = in some function definitions
  • Next by Date: Re: Using ReplaceAll (/.) on numerical digits
  • Previous by thread: Re: := vs = in some function definitions
  • Next by thread: Re: := vs = in some function definitions