RE: Holding Arguments in a Second Argument List

• To: mathgroup at smc.vnet.net
• Subject: [mg32296] RE: [mg32238] Holding Arguments in a Second Argument List
• From: "David Park" <djmp at earthlink.net>
• Date: Fri, 11 Jan 2002 04:35:35 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

Thanks Andrzej, Allan, Hartmut and Jens for your various solutions. Allan's
solution worked out best for me.

Here is a little sample routine to show how it works. I am writing routines
to do various operations on an extended matrix structure, so the actual
routines are a little more complicated than below. Since I want to reassign
parts of the structure, the passed matrix symbol must be unevaluated. If the
input arguments are in error I want to give error messages and Abort, which
is easy enough. But sometimes I don't want to issue messages or abort the
whole operation, but only return a \$Failed to the calling routine which can
then handle it.

The following routine scales a row of a matrix by a scale factor, but
returns \$Failed if a nonexistent row is specified.

scalerow[row_, scale_] :=
Function[matrix,
Catch[
Module[{r},
r = First[Dimensions[matrix]];
If[ !1 <= row <= r, Throw[\$Failed]];
matrix[[row]] =
scale*matrix[[row]];]],
{HoldFirst}]

testmat = {{1, 2}, {4, 2}};

This is OK...

mat = testmat;
mat // scalerow[1, 2]
mat
{{2, 4}, {4, 2}}

This fails because there is no row 3

mat = testmat;
mat // scalerow[3, 2]
mat
\$Failed
{{1, 2}, {4, 2}}

David Park

> From: David Park [mailto:djmp at earthlink.net]
To: mathgroup at smc.vnet.net
>
> Dear MathGroup,
>
> I have a series of procedures which manipulate a matrix. I like to define
> them in this fashion.
>
> function[args___][matrix_] := ... because then I can write lines like
>
> matrix // function[args...] etc.
>
> But I have to define the function so that matrix is held, and I don't want
> to write Unevaluated with every use. But Attributes = {HoldFirst} only
> applies to the first argument list. Is there a direct way to define the
> attributes of a second argument list?
>
> In any case, I write the routines this way.
>
> function[args___]:=
> 	Function[matrix,
> 		Module[{}, code],
> 		{HoldFirst}]
>
> and that works. The code usually changes the matrix and I display it
> separately. But sometimes the operation will fail and then I would like
>
> function[args...][matrix]
>
> to return \$Failed. But I am unable to do that. The best I seem to
> be able to
> do is return Return[\$Failed].
>
> Here is a sample routine which just tries to return \$Failed.
>
> foo[mathgroup_] :=
>   Function[mat,
>     Module[{},
>         If[Not[mathgroup === great], Return[\$Failed]]],
>     {HoldFirst}]
>
> foo[x][mat]
> Return[\$Failed]
>
> Can anyone tell me how to get rid of the Return wrapper?
>
> Thanks
>
> David Park
> djmp at earthlink.net