1
$\begingroup$

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};

enter image description here

Now how to do same thing with Set

Thanks

$\endgroup$
  • $\begingroup$ @Guess who it is, nice catch. It is embarrassing to make mistake with function name that you continually use. -:) $\endgroup$ – Algohi Jul 13 '15 at 10:11
  • 1
    $\begingroup$ You can actually just DownValues[h]={HoldPattern[h[x_, y_]] -> 2 x t}; to emulate Set. When you look at DownValues, 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 because t 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 with Information or Defitnition. I think I once asked about where this information is stored, but ... $\endgroup$ – Szabolcs Jul 13 '15 at 11:40
  • $\begingroup$ ... I forgot the answer and cannot find the questin anymore. $\endgroup$ – Szabolcs Jul 13 '15 at 11:41
  • $\begingroup$ @Szabolcs that explain things better. When you say MMA trucks whether a DownValues definition was mad with = or :=, what makes difference then if at the end the substitution will look at DownValues only? In another words why does MMA keep truck the definitions? $\endgroup$ – Algohi Jul 14 '15 at 9:56
  • $\begingroup$ Strongly related SO thread: stackoverflow.com/q/5919284/590388 $\endgroup$ – Alexey Popkov Nov 10 '15 at 12:08
1
$\begingroup$

To do same thing with Set

DownValues[h] = {First[Hold[HoldPattern[h[x_, y_]] :> 2 x t] /. t -> x + y ]};

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.