Re: := vs = in some function definitions
- To: mathgroup at smc.vnet.net
- Subject: [mg113392] Re: := vs = in some function definitions
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Wed, 27 Oct 2010 05:14:10 -0400 (EDT)
After defining testFunction look at
?testFunction
to see the stored definition.
Bob Hanlon
---- Sam Takoy <sam.takoy at yahoo.com> wrote:
=============
On 10/22/2010 1:38 AM, Albert Retey 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?
>
> I think there have been many good answers to your question already, but
> if you are interested in what is going on I think it worth to mention
> that a function definition is nothing but a definition of a global
> replacement rule in Mathematica. And of course you can look at these
> rules and that might be interesting if you try to understand what Set
> and SetDelayed actually do (so no more guessing is needed).
>
> Have a look at the DownValues (or OwnValues) to see what happens in the
> two cases:
>
> f[x_] = IdentityMatrix[50].Append[Range[1, 49], x];
> DownValues[f]
> g[x_] := IdentityMatrix[50].Append[Range[1, 49], x];
> DownValues[g]
>
> it is instructive to also evaluate this:
>
> x = 1
> f[x_] = IdentityMatrix[50].Append[Range[1, 49], x];
> DownValues[f]
>
> hth,
>
> albert
>
>
Thanks for all the responses! Very insightful.
Here's one more thing I can't figure out:
testFunction[f_][x_, y_] = Module[{},
Print[f[x, y]];
5
];
testFunction[ArcTan][x, y]
Why doesn't this function print out ArcTan[x, y]?
Thanks,
Sam