Of course we know



But I try to compress my expression into a string

com = Compress;
SetAttributes[com, HoldAllComplete]
string = com[Range[10]]


I cannot get a unevaluted expression from it?

Defer /@ Uncompress[string]


Actually the Range[20] is the expected output.How to do this?

  • $\begingroup$ Anyone can add a appropriate tag for this question?I cannot think out of it.. $\endgroup$
    – yode
    Commented Apr 10, 2017 at 16:50
  • 4
    $\begingroup$ Try this: myCompress = Function[x, Compress[Unevaluated[x]], HoldAll]; test = cc[Range[10]]; Uncompress[test, Defer]. $\endgroup$ Commented Apr 10, 2017 at 16:55
  • $\begingroup$ @J.M. Your comment always is a good answer. :) $\endgroup$
    – yode
    Commented Apr 10, 2017 at 17:06
  • $\begingroup$ @xzczd Thanks for warmth,but it deserve function-construction? $\endgroup$
    – yode
    Commented Apr 10, 2017 at 17:09
  • 1
    $\begingroup$ I think your question fits the description of the tag. (In my eyes, your question is "why doesn't my function com work?" ) Feel free to remove it if you think it's improper. BTW if you're not interested in defining a function for the task, simply use Compress@Unevaluated@Range[20]. $\endgroup$
    – xzczd
    Commented Apr 10, 2017 at 17:19

1 Answer 1


You need to wrap the expression Unevaluated before passing it to Compress, which means defining your own function instead of just assigning Compress to com:


SetAttributes[com, HoldAllComplete]; 
com[expr_] := Compress[Unevaluated[expr]];

string = com[Range[10]] 

And then make sure to wrap it in Hold or Defer when you pull it back out, which you can do with the second argument of Uncompress:

Uncompress[string, Defer]    
(* Range[10] *)
  • 1
    $\begingroup$ Ah, we were thinking the same thing... +1! $\endgroup$ Commented Apr 10, 2017 at 16:58
  • $\begingroup$ Good lesson,thanks.. $\endgroup$
    – yode
    Commented Apr 10, 2017 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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