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.

This question already has an answer here:

I want to convert {Hold[1 + 2], Hold[3 + 4], Hold[5*6]} into Hold[{1 + 2, 3 + 4, 5*6}].

I have tried

{Hold[1 + 2], Hold[3 + 4],  Hold[5*6]} /. {Hold[a_], Hold[b_], Hold[c_]} -> Hold[{a, b, c}]
Hold[{1 + 2, 3 + 4, 5 6}]

Is there a simpler way?

share|improve this question
1  
Related (perhaps duplicate): Join held lists, Injecting Sequence into Hold. –  István Zachar Oct 26 '13 at 7:07
add comment

marked as duplicate by Kuba, halirutan, Sjoerd C. de Vries, Artes, m_goldberg Oct 26 '13 at 13:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 4 down vote accepted

Join works with every Head so it seems to be the best way here:

Join @@ {Hold[1 + 2], Hold[3 + 4], Hold[5*6]}
Hold[1 + 2, 3 + 4, 5 6]

and if you really need those {} inside:

% /. Hold[x__] :> Hold[{x}]
Hold[{1 + 2, 3 + 4, 5 6}]
share|improve this answer
add comment

One way which works no matter how many elements your list has is to use Flatten

expr = {Hold[1 + 2], Hold[3 + 4], Hold[5*6]};
Flatten[Hold @@ expr, 1, Hold]

(* Hold[1 + 2, 3 + 4, 5 6] *)

You see that the list braces are not created. They could be reconstructed too but the question is whether you really need them. I assume for your purpose it is not really a difference whether you have List[e1,e2,...] or Hold[e1,e2,...] but Hold has the advantage of not evaluating your expression.

If you want the list, you can add another rule

expr /. {a__Hold} :> Flatten[Hold[a], 1, Hold] /. 
 Hold[a__] :> Hold[{a}]

(* Hold[{1 + 2, 3 + 4, 5 6}] *)

Or you define a function which basically does the same but the rules are hidden in the DownValues

f[{a__}] := f[a];
f[a__Hold] := f[Flatten[Hold[a], 1, Hold]];
f[Hold[a__]] := Hold[{a}];

f[expr]

(* Hold[{1 + 2, 3 + 4, 5 6}] *)
share|improve this answer
add comment

Here is an interesting way:

expr = {Hold[1 + 2], Hold[3 + 4], Hold[5*6]};
expr /. {Hold -> Sequence, List -> Hold}

Hold[1 + 2, 3 + 4, 5 6]

And if you want the inner List back:

expr /. {Hold -> Sequence, List -> Hold} /. Hold[x__] :> Hold[{x}]
Hold[{1 + 2, 3 + 4, 5 6}]
share|improve this answer
add comment

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