# How to Map a function with HoldAll attribute

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var];
SetAttributes[f, HoldAll];

f[x]
(* False *)

x = 10;
f[x]
(* True *)


It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)


Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)


The problem is that Map evaluates x and pass the value of x to f, so ValueQ1 gives false. How to get the Map to give the right sequence {True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)


but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

EDIT II Somehow solutions provided by Mr.Wizard, Kuba and Algohi work fine in my case, but are not so easy as to add Unevaluated in the Map inside the f function, as suggested by kugler.

• I think my answer anticipated your update. :-) If you find in inadequate please update your question one again to explain why. I'll be back later today to check. – Mr.Wizard Mar 5 '15 at 20:25
• Thanks, I'm trying to understand if your solution works for my real case. I'll add a feedback as soon as possible (consider here is quite late, I'm in Italy). – bobknight Mar 5 '15 at 20:27
• Regarding your EDIT II if you can give a more representative example of your function I will attempt to help you apply my method with the least effort or complication possible. – Mr.Wizard Mar 6 '15 at 2:40
• I really appreciate your help, but the actual code is very long. It's a webMathematica application with a couple of packages calling each other. Basically, what I need is to pass input variables collected from a webMathematica page to an inner function (not directly coded into the page) to check if they have a value and if they are of expected type. In the inner function I use MSPValueQ (similar to ValueQ) and I need to pass the variables unevaluated. Thus, my efforts to simplify the question and avoid to put many lines of code that will make very hard to understand the core issue. – bobknight Mar 6 '15 at 3:12

Map[f, Unevaluated@{x, y, z}]
(* {True, True, True} *)


Update:

ClearAll[f, x, y, z, q, w, e, r, t]
f[vars : {(_) ..}] := Map[ValueQ, Unevaluated@vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {True} *)

f[{x, y}]
(* {True, False} *)

f[{x, y, z, q, w, e, r, t}]
(* {True, False, False, False, False, False, False, False} *)

• thanks for the quick answer, unfortunately it works for the simplified example I posted but doesn't for the actual code. I'm trying to change the question so to post another piece of code to be more clear and precise. – bobknight Mar 5 '15 at 19:37
• @bobknight I look forward to your clarifying update. – Mr.Wizard Mar 5 '15 at 19:54
• @bobknight how about using Unevaluated in the definition of f, i.e., f[vars : {(_) ..}] := Map[ValueQ, Unevaluated@vars];? – kglr Mar 5 '15 at 21:15
• Finally, that's the best way to solve the problem! Thanks a lot. Solutions provided by Mr.Wizard, Kuba and Algohi also have the right behavior but are not so easy as this one. – bobknight Mar 6 '15 at 1:17
• @bobknight 1) so do you want this to work? vars := {x, y, z, q, w, e, r, t}; f[vars] 2) it would be nice to comment or whatever. My answer looks like I missed the point, no comments, no votes, and now I see it is correct :P. Just a note for future. – Kuba Mar 6 '15 at 6:24

Or set another attribute, Listable and be done with Map:

SetAttributes[f,{HoldAll,Listable}];

x=10; y=.; z=.;
f[{x,y,z}]
(* {True, False, False} *)

• Beat me to it. +1 – Mr.Wizard Mar 5 '15 at 19:53

If you can't use Listable to your code is probably to use:

ClearAll[f]

(*first definition handles cases when you use list:={x,y};f[list]*)
f[listof_Symbol] := f @@ (Hold[listof] /. OwnValues[listof]);

(*general definition handles list of symbols*)
f[vars_ : {(_) ..}] := ReleaseHold @ Map[ValueQ, Hold[vars], {2}];

(*as in kugler's answer the line above can be written as:
Map[ValueQ, Unevaluated[vars]];
*)

SetAttributes[f, HoldAllComplete];

ClearAll[x,y]

f[{x}]

{False}

x = 10; f[{x}]

 {True}

f[{Unevaluated@x, Unevaluated@y}]

{True, False}

f[{x, y}]

{True, False}

list := {x, y};
f[list]

{True, False}

• Yes, it works. Sorry, I didn't commented your solution yesterday night, because it was quite late and I was sleeping on the keyboard :-) – bobknight Mar 6 '15 at 7:28

The two standard approaches to this problem, Unevaluated and Listable, have already been posted.

If you need a different kind of evaluation control please consider my original method:(1),(2),(3),(4)

Load step How do I evaluate only one step of an expression? then:

SetAttributes[f2, HoldAll]

f2[s_Symbol] := step[s] /. {_[x_List] :> f2 /@ Unevaluated[x], _ :> ValueQ[s]}


Now:

x = 1;
q := {x, y, z};

f2[q]

{True, False, False}


you can change the attribute of Map:

Clear[f]
f[var_] := ValueQ[var]

SetAttributes[f, HoldAll]
SetAttributes[Map, HoldAll]
{x, y, z} = {1, 2, 3};
Map[f, {x, y, z, k}]

(*{True, True, True, False}*)

• The down-vote is not mine. However I can understand it as this is dangerous. One would do better to create a new Map derivative using InternalInheritedBlock if this is desired, but using Unevaluated` is simpler. – Mr.Wizard Mar 5 '15 at 20:21
• @Mr.Wizard the OP replied that Unevaluated did not work for him. also I believe this can be done locally if the case is that it creates global issue – Algohi Mar 5 '15 at 20:24
• I suggest that you include that local method. – Mr.Wizard Mar 5 '15 at 20:26
• I believe this could be don but I don't know how. – Algohi Mar 5 '15 at 20:29
• Oh, I understand. :-) – Mr.Wizard Mar 5 '15 at 20:33