MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Orderless attribute for named functions and function arguments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg72534] Re: Orderless attribute for named functions and function arguments
  • From: dh <dh at metrohm.ch>
  • Date: Fri, 5 Jan 2007 02:10:11 -0500 (EST)
  • References: <enip11$hf9$1@smc.vnet.net>


Hi Hannes,

it is not clear if you want to use the syntax: "f[functionName_String]" 

simply as a name or as a function. The former is against the rules how a 

name  must be formed, "[" and "]" are not allowed. The latter works if 

you give the returned function the attribute Orderless. E.g:

f[]:=Module[{a},

     SetAttributes[a,Orderless];

     a[x1_,x2_]:=x1/x2;

     a

     ]



Daniel



Hannes Kessler wrote:

> Dear experts,

> 

> a way to use the attibute Orderless that works for functions with named

> arguments in the form of rules is

> 

> SetAttributes[f,Orderless];

> f[

>    "Named argument 1" -> x_,

>    "Named argument 2" -> y_,

>    "Named argument 3" -> z_]:=

> Module[{xx, yy, zz},

>   {xx, yy, zz} = {x, y, z};

>   (*Do something with xx, yy, zz and return the result*)

>    ...];

> 

> Calling the function with arguments in different ways gives identical

> results.

> No need to care about the order of the named arguments:

> 

> f1 = f[

>    "Named argument 1" -> 1,

>    "Named argument 2" -> 2,

>    "Named argument 3" -> 3];

> f2 = f[

>    "Named argument 2" -> 2,

>    "Named argument 1" -> 1,

>    "Named argument 3" -> 3];

> f1 == f2

> --> True

> 

> Now I want an additional dependence of the result on a name of the

> function in the following form:

> 

> f[functionName_String][

>    "Named argument 1" -> x_,

>    "Named argument 2" -> y_,

>    "Named argument 3" -> z_]:=

> Module[{xx, yy, zz},

>   {xx, yy, zz} = {x, y, z};

>   (*Do something with xx, yy, zz*)

>    ...;

>    (*Return the result depending on functionName*)

>    Which[

>      MatchQ[functionName, "func1"],  value1,

>      MatchQ[functionName, "func2"],  value2,

>      ...]];

> 

> However, it is not possible anymore to assign the attribute Orderless

> as

> 

> SetAttributes[f[functionName_String],Orderless];

> 

> generates an error message:

> 

> SetAttributes::sym: Argument f[functionName_String] at position 1 is

> expected \

> to be a symbol.

> 

> A simple solution is to reverse the definition of f:

> 

> SetAttributes[f,Orderless];

> f[

>    "Named argument 1" -> x_,

>    "Named argument 2" -> y_,

>    "Named argument 3" -> z_][functionName_String]:=...

> 

> Is there another way to reach the same goal? Reversing the definition

> of f is of course completely acceptable in most cases. Nevertheless, in

> some cases it may preferrable to view f[functionName] as different

> functions specified by the string functionName, and the arguments to

> these functions should be given in any order.

> 

> Thanks in advance,

> Hannes Kessler

> 



  • Prev by Date: Re: Finding paths in graphs
  • Next by Date: Re: please explain why numerical integration is attempted
  • Previous by thread: Re: Orderless attribute for named functions and function arguments
  • Next by thread: Re: Orderless attribute for named functions and function arguments