I'm not sure what the "nth" argument is supposed to be, since n
does not appear in the OP, just 2
. So here's a way to get the name of a Symbol
argument for a fixed n
, as in the OP:
plo // ClearAll;
SetAttributes[plo, HoldAll];
With[{nMinusOne = 2 - 1},
plo[Repeated[_, {nMinusOne, nMinusOne}], arg_, ___] :=
SymbolName@Unevaluated@arg;
]
Block[{bb = 3}, plo[aa, bb, cc, dd]]
(* "bb" *)
Another way; this time with an adjustable n
depending on a global variable $myN
(one could make it an argument to plo[n, fun]
but the need is unclear):
plo // ClearAll;
SetAttributes[plo, HoldAll];
$myN = 2;
plo[fun__] := With[{n = $myN},
Function[, SymbolName@Unevaluated@Slot[n], HoldAll][fun]
];
Block[{bb = 3, cc = 7, $myN = 3}, plo[aa, bb, cc, dd]]
(* "cc" *)
After seeing the OP's application of this problem, I thought of this way to get the output of the OP's answer without having to use Part
:
getArgs // ClearAll;
getArgs // Attributes = {HoldAll};
getArgs[fun__] := AssociationThread[
List @@ SymbolName /@ Unevaluated /@ Hold[fun] -> {fun}];
Block[{bb = 3}, getArgs[aa, bb, cc, dd]]
(* <|"aa" -> aa, "bb" -> 3, "cc" -> cc, "dd" -> dd|> *)
(ToString[#, InputForm] &)
might be a better transformation of the arguments to strings than SymbolName
(for both getArgs[]
and plo[]
). It's rare that the arguments in my function calls are all symbols.
Also getArgs[f_[fun___]] :=...
seems more likely to be useful.
Plo[fun__] := With[{tmp = Part[List @@@ Unevaluated @@ {Hold[fun]}, {1}, 2]}, SymbolName[tmp]]
. $\endgroup$