Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

enter image description here

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
share|improve this question
2  
Unless @Mr.Wizard has answered your question adequately, can you clarify? I'm confused by the lack of a verb in the question title and the relationship between the green text and code. –  bobthechemist Nov 27 '13 at 16:50
2  
I don't think this question needs to be closed. As I see it the user wants to show the evaluation chain, including at least the step where symbol is "filled in" by a definition, such as x in x^2 + 3. User: is this correct? –  Mr.Wizard Nov 28 '13 at 2:29

1 Answer 1

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}
share|improve this answer
    
I think your answer might be closer to what the OP is asking for if you added a definition for his 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 2:15
    
@m_goldberg Good point. user10833: that is indeed the way to package an expression such as this as a function. I did not do that as I did not feel this was complete, e.g. there is no 2^2+3 step shown. –  Mr.Wizard Nov 28 '13 at 2:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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