
How can I create a function F that works like this:
x = 2
f1 = x^2 + 3
f2=x+5
F[f1]
x^2 + 3 = 2^2 + 3 = 7
F[f2]
x + 5 = 2 + 5 = 7

How can I create a function F that works like this:
x = 2
f1 = x^2 + 3
f2=x+5
F[f1]
x^2 + 3 = 2^2 + 3 = 7
F[f2]
x + 5 = 2 + 5 = 7
First you will need to make your definitions with SetDelayed (:=) rather than Set (=):
x := 2
f1 := x^2 + 3
f2 := x + 5
You can get a list of all evaluation steps using Trace or TracePrint. To get only the steps that transform the entire expression use the Option TraceDepth -> 1, and you can format with Row
Row[Trace[f1, TraceDepth -> 1], "="]
f1 = x^2+3 = 4+3 = 7
This is not exactly what you asked for but I hope it is close enough to help. The additional steps are accessible with Trace but I could not think of a simple and robust way to use them and I can't spend more time on this right now. For reference:
Trace[f1]
{f1,x^2+3,{{x,2},2^2,4},4+3,7}
F along the lines of: SetAttributes[F, HoldFirst]; F[var_Symbol] := Row[Trace[var, TraceDepth -> 1], "="] -- providing, of course, that you think this would be robust enough to meet the OP needs.
– m_goldberg
Nov 28 '13 at 02:15
2^2+3 step shown.
– Mr.Wizard
Nov 28 '13 at 02:24
xinx^2 + 3. User: is this correct? – Mr.Wizard Nov 28 '13 at 02:29