4
$\begingroup$

If you have a symbol with a value, you can clear it with Remove[]. Thus

a=1;
Remove[a];
a(*returns unevaluated, as it has no value*)

But if you have a list of symbols with values, it does not seem to work to map Remove[] across them. Thus

a = 1; b = 2; c = 3;
Map[Remove[#] &, {a, b, c}];
a (* returns 1, as the variable was never cleared *)

Is there a good reason why this isn't working?

$\endgroup$
  • 2
    $\begingroup$ Very good reason: after a = 1, a has the value 1. So you essentially do Remove[1] - change Remove to some generic f and see how it woks. $\endgroup$ – corey979 Nov 18 '16 at 18:49
  • 2
    $\begingroup$ In the second case the list {a,b,c} gets evaluated to {1,2,3} first, so Remove only gets the numbers, as you can see from the errors , "Remove::ssym: 1 is not a symbol. >>" $\endgroup$ – george2079 Nov 18 '16 at 18:49
  • 5
    $\begingroup$ I noticed that many people here use f[#]& /@ ... instead of f /@ .... I'm not sure why. Not only is it unnecessarily complicated, it also effectively removes any Hold* attributes the function may have. $\endgroup$ – Szabolcs Nov 18 '16 at 21:43
  • $\begingroup$ @Szabolcs Turns out it's a habit. $\endgroup$ – corey979 Nov 18 '16 at 22:32
9
$\begingroup$

As stated in the comments, a is evaluated before being fed into Remove. We can prevent this by using Unevaluated:

a = 1; b = 2; c = 3;
Remove /@ Unevaluated[{a, b, c}];
a
a

One can also use Apply instead of Map to effectively achieve Remove[a, b, c]:

a = 1; b = 2; c = 3;
Remove @@ Unevaluated[{a, b, c}];
a
a
$\endgroup$
  • 2
    $\begingroup$ Or just Remove[a, b, c] $\endgroup$ – Bob Hanlon Nov 18 '16 at 19:02
  • $\begingroup$ @BobHanlon Yes, good point. I assume OP is in some situation (not sure how) where he can't manually type out the variables. $\endgroup$ – Chip Hurst Nov 18 '16 at 20: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.