# Setting a function equal to another function then Clearing the latter

I have a loop in which several values of a function are computed, depending on the values for that function that were computed in the previous iteration of the loop. This could be implemented recursively, but it would result in very many downvalues for the function. I want to save the memory that those downvalues would occupy. My plan is to maintain two functions; to overwrite the old one with the new one in each iteration of the loop; to clear the new function, and then to compute the values of the new function depending on the values of the old function.

But unfortunately in spite of my using "Set" instead of "SetDelayed", it seems that as soon as the new function is Cleared, the old function forgets its definition:

Clear[funA, funB]
funA[3] = 7
funB = funA
funB[3]
Clear[funA]
funB[3]


gives the output:

7
funA
7
funA[3]


all of which is what I wanted, except that I want the last output to be "7", not "funA[3]".

I have experimented with "SetDelayed" (:=) instead of "Set" (=) and with "Evaluate", but to no avail. What am I doing wrong ?

I have a version of my code that works perfectly, in which instead of having two functions and Clearing the old function, I have a single function, and in each iteration of the loop I "Unset" its values that are no longer required. However, the Unsetting process goes value-by-value in a loop of its own, and takes quite a while.

Try this instead of funB = funA:
DownValues[funB] = DownValues[funA] /. funA -> funB

Or even better: Use Associations.
• The /. funA -> funB is not mysterious at all. Have a look at DownValues[funA]. Then you see that the downvalues are stored as delayed rules like {HoldPattern[funA[3]] :> 7}. If you want that rule to apply for funB, then you have to do the replacement. Jun 4, 2020 at 22:57