Re: := vs = in some function definitions
- To: mathgroup at smc.vnet.net
- Subject: [mg113371] Re: := vs = in some function definitions
- From: Sam Takoy <sam.takoy at yahoo.com>
- Date: Tue, 26 Oct 2010 05:32:20 -0400 (EDT)
- References: <i9m7u7$j41$1@smc.vnet.net> <i9r80e$hp5$1@smc.vnet.net>
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