In the context of writing something like homework solutions for a math class, I often face an awkward situation. Each problem will have some idiomatic functions that are cumbersome and error prone to rewrite. For example,
One solution, to avoid rewriting the right hand side is
This allows me to write out the right hand side in its entirety with less chance of typos, when I don't need to manipulate the subparts of the expression, making for cleaner code. However in some cases, the function
f may take on different definitions in different problems, so I would often need to redefine it.
Is there a way to make a definition local to some part of the code so that the redefinition will have less chance of affecting the other parts of my code in case I forget to redefine the variable? This is especially important if formatting the function requires some manual spacing adjustments which make the code cluttered.