# How to get a unevaluted expression from a compressed string?

Of course we know

Defer[Range[20]]


Range[20]

But I try to compress my expression into a string

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


1:eJxTTMoPSuNiYGAoZgESPpnFJZ6MQIYhmDQCk8Zg0gRMmoJJMzBpDiYtwKSlJxNIlwEApm8I6w==

I cannot get a unevaluted expression from it?

Defer /@ Uncompress[string]


{1,2,3,4,5,6,7,8,9,10}

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

• Anyone can add a appropriate tag for this question?I cannot think out of it..
– yode
Commented Apr 10, 2017 at 16:50
• Try this: myCompress = Function[x, Compress[Unevaluated[x]], HoldAll]; test = cc[Range[10]]; Uncompress[test, Defer]. Commented Apr 10, 2017 at 16:55
– yode
Commented Apr 10, 2017 at 17:06
• @xzczd Thanks for warmth,but it deserve function-construction?
– yode
Commented Apr 10, 2017 at 17:09
• 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]. Commented Apr 10, 2017 at 17:19

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:

ClearAll[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]