It is known from several questions asked in SE that one of the basic differences between Set
and SetDelayed
is that the former evaluates the right hand side at the time of assignment whereas the other is not.
When I looked at the DownValues
in both cases I found that both definitions result into same DownValues
structures.
For example:
t = x + y
f[x_, y_] := 2 x t
g[x_, y_] = 2 x t
Now:
DownValues/@{f,g}
(*{{HoldPattern[f[x_, y_]] :> 2 x t}, {HoldPattern[g[x_, y_]] :> 2 x (x + y)}}*)
As you can see here that both Set
and SetDelayed
transformed into same DownValues
structures (HoldPattern[function pattern] :> right hand side
)
My questions are:
1-Is it correct that internally both will be treated equally based on DownValues
structure.
2- If I want to define function using SetDelayed
I can do it like this:
DownValues[h]={HoldPattern[h[x_, y_]] :> 2 x t};
Now how to do same thing with Set
Thanks
DownValues[h]={HoldPattern[h[x_, y_]] -> 2 x t};
to emulateSet
. When you look atDownValues
, it will always be shown as:>
and never as->
. But when you set it you can use->
. When storing it,:>
is necessary because the downvalue should not change just becauset
was assigned a different value after the downvalue was set. Mathematica does keep track of whether a (downvalue) definition was made with=
or:=
, and does print it differently withInformation
orDefitnition
. I think I once asked about where this information is stored, but ... $\endgroup$DownValues
definition was mad with=
or:=
, what makes difference then if at the end the substitution will look atDownValues
only? In another words why does MMA keep truck the definitions? $\endgroup$