I would like to reference by index Held arguments. For example:
Attributes[ func ]={ HoldAllComplete };
func[ args__ ]:=Module[ { argsList,arg1 },
(* In the function I would like to reference the Held args by indexing (e.g. args[[1]] ),
but anyway I try results in the args being evaluated. *)
argsList=List[ args ] (* causes the args to be evaluated *)
arg1=argsList[[1]] (* gives the evaluated arg 1 *)
]
I have spent some time looking at docs, but can’t find anything which discusses this.
Any suggestions as to where to look will be appreciated.
Try something like this:
In[126]:= ClearAll@f;
Attributes[f] = HoldAll;
f[x__] := Module[{list = Hold @ Unevaluated @ x},
Print["there are ", Length @ Unevaluated @ x, " arguments in total"];
Print["this is the first argument: ", Part[list, {1}, 1]];
Print["this is the evaluated first argument: ",
ReleaseHold[Part[list, {1}, 1]]
];
Print["this is the third argument: ", Part[list, {1}, 3]];
Print["this is the evaluated third argument: ",
ReleaseHold[Part[list, {1}, 3]]
];
foo
]
In[129]:= f[2 + 1, 2, Echo@3, 4]
During evaluation of In[129]:= there are 4 arguments in total
During evaluation of In[129]:= this is the first argument: Hold[2+1]
During evaluation of In[129]:= this is the evaluated first argument: 3
During evaluation of In[129]:= this is the third argument: Hold[Echo[3]]
(*echoing: 3*)
During evaluation of In[129]:= this is the evaluated third argument: 3
Out[129]= foo
Thanks, I love example code!
I’m new to Wolfram Language, so much to learn, but I learn best by example.