This question is hard to phrase and even harder to google. Maybe it's best to explain it with a very simple example.

squareList = # # & /@ Range[10]
totalRoot = Sqrt[Total[squareList]]

Now I want to apply some kind of function to totalRoot to get the following:

Sqrt[Total[# # & /@ Range[10]]]

Hopefully you understand what I mean. I'll try to put it into words. I want a function that, given a variable or an expression, returns an expression equivalent to it, but written only using built in functions. Basically, it would substitute back any variable declaration.

In case anyone is interest in why I need this, it's because it makes it easier to share the code of very simple programs. I am still a beginner when it comes to Mathematica so I tend to write all my programs in little steps, declaring a variable for each one.

  • $\begingroup$ Writing programs in small steps and assigning a symbol to each step is what everyone does while learning or trying to explain something to non experts. Continue doing that and you'll begin to feel more comfortable and confident on your code in a while. There's nothing bad in it $\endgroup$ – Dr. belisarius Feb 24 '15 at 23:29
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Feb 24 '15 at 23:30

There are several ways to do this, but here is a simple one to get you started. One thing to fix first, though, is to use delayed assignments in your variable declarations - otherwise the variables will store already evaluated results. So:

squareList := # # & /@ Range[10]
totalRoot := Sqrt[Total[squareList]]

Here is a simple function, then. It assumes you only want to expand variables, and that all your variables live in the Global` context.

SetAttributes[expandAll, HoldAll];
expandAll[expr_] :=
   HoldComplete[expr] //. Flatten[ 
      Map[ToExpression[#, StandardForm, OwnValues] &] @ Names["Global`*"]

So that


(* HoldComplete[Sqrt[Total[(#1 #1 &) /@ Range[10]]]] *)

Note that one has to wrap the result into some wrapper that would hold the code and prevent it from evaluation, such as Hold or HoldComplete. Or, you can also use Defer:

Defer @@ expandAll[totalRoot]

(* Sqrt[Total[(#1 #1 &) /@ Range[10]]] *)
| improve this answer | |
  • $\begingroup$ Thank you very much! This is exactly what I wanted. $\endgroup$ – user26610 Feb 25 '15 at 12:16
  • $\begingroup$ @user26610 Was glad to help. Thanks for the accept. $\endgroup$ – Leonid Shifrin Feb 25 '15 at 12:46

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.