Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.4K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Referencing held arguments

Posted 4 years ago

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.

POSTED BY: Terry Stewart
2 Replies
Posted 4 years ago

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
POSTED BY: Updating Name

Thanks, I love example code! I'm new to Wolfram Language, so much to learn, but I learn best by example.

POSTED BY: Terry Stewart
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard