Merging two held expressions into a held compound expression - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-19T20:03:20Z https://mathematica.stackexchange.com/feeds/question/43291 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/43291 2 Merging two held expressions into a held compound expression zorank https://mathematica.stackexchange.com/users/5856 2014-03-03T14:07:00Z 2014-03-04T10:52:42Z <p>Consider</p> <pre><code>e1 = Hold[a = x + x; b = y + y + y]; e2 = Hold[c = a + a + b + b + b; c^2]; </code></pre> <p>How can I obtain the following?</p> <pre><code>e12 = Hold[a = x + x; b = y + y + y; c = a + a + b + b + b; c^2] </code></pre> <p>For example,</p> <pre><code>e1 ~Join~ e2 </code></pre> <p>does not work since it gives one "," sign in the middle. another attempt</p> <pre><code>With[{e1temp = e1, e2temp = e2}, Hold[e1temp; e2temp]] </code></pre> <p>gives nested <code>Hold</code> calls that at the end do not want to evaluate (and are hard to get rid off):</p> <blockquote> <pre><code>Hold[Hold[a = x + x; b = y + y + y]; Hold[c = a + a + b + b + b; c^2]]] </code></pre> </blockquote> <p>I am amazed that such a simple thing is causing me so much pain. :)</p> <h3>Edit</h3> <p>My current solution.</p> <pre><code>merge[Hold[expr1_], Hold[expr2_]] := Hold[expr1; expr2] merge[e1, e2] </code></pre> <blockquote> <pre><code>Hold[(a = x + x; b = y + y + y); (c = a + a + b + b + b; c^2)] </code></pre> </blockquote> https://mathematica.stackexchange.com/questions/43291/-/43295#43295 5 Answer by Simon Woods for Merging two held expressions into a held compound expression Simon Woods https://mathematica.stackexchange.com/users/862 2014-03-03T15:00:39Z 2014-03-03T15:00:39Z <p>I'm not sure why you don't like your <code>merge</code> function. This is the same pattern matching approach but using <code>ReplaceAll</code>:</p> <pre><code>{e1, e2} /. {Hold[x_], Hold[y_]} :&gt; Hold[x; y] </code></pre> <p>If you would rather avoid patterns here is one (ugly) option:</p> <pre><code>Block[{CompoundExpression}, (e1; e2) ~Thread~ CompoundExpression ~Thread~ Hold] </code></pre> https://mathematica.stackexchange.com/questions/43291/-/43366#43366 1 Answer by Albert Retey for Merging two held expressions into a held compound expression Albert Retey https://mathematica.stackexchange.com/users/169 2014-03-04T10:52:42Z 2014-03-04T10:52:42Z <p>I think there is nothing wrong with the solutions you have. If for whatever reason you'd really need to join into one <code>CompoundExression</code> instead of nesting three of them, then here are two way to achieve that:</p> <pre><code>CompoundExpression @@@ Join[ Hold @@@ Hold[a = x + x; b = y + y + y], Hold @@@ Hold[c = a + a + b + b + b; c^2], 2 ] </code></pre> <p>Many functions which operate on "generalized" lists have a level argument and so does <code>Join</code>. Unfortunately it wants the heads of all levels above the one it operates to be the same which makes necessary to exchange the <code>CompoundExpression</code> by <code>Hold</code> temporarily for you example and the above a little more complicated than one would wish...</p> <p>Of course pattern matching is also an options which almost always will provide a solution:</p> <pre><code>Replace[ {Hold[a = x + x; b = y + y + y], Hold[c = a + a + b + b + b; c^2]}, {Hold[CompoundExpression[cp1__]], Hold[CompoundExpression[cp2__]]} :&gt; Hold[CompoundExpression[cp1, cp2]] ] </code></pre>