Re: Orderless indexed functions
- To: mathgroup at smc.vnet.net
- Subject: [mg20890] Re: [mg20874] Orderless indexed functions
- From: Hartmut Wolf <hwolf at debis.com>
- Date: Sat, 20 Nov 1999 01:07:04 -0500 (EST)
- Organization: debis Systemhaus
- References: <199911190146.UAA01488@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
N Monomachoff schrieb:
>
> Hello,
>
> I need to use 'indexed functions' like f[0][x__] and f[1][x__].
> It's important that the functions be Orderless, but
>
> SetAttributes[f[0],Orderless]
>
> isn't accepted because f[0] isn't a symbol, and
>
> SetAttributes[f,Orderless]
>
> doesn't affect the ordering in the second set of brackets.
> How does one make a nonsymbolic head orderless?
>
Dear N Monomachoff,
I don't know, and if someone does, then please forget these tricks:
(1) use an ancillary symbol
This can be used when you define your function. Wrap the definition
(SetDelayed) in makeOrderless and that definition will be attached to an
(invisible) Symbol *that* is made Orderless. The rule for your function
just leads to that symbol.
In[1]:= SetAttributes[makeOrderless, HoldAll]
In[2]:=
makeOrderless[sym_Symbol[tags___][cseq___] := rhs_] :=
Block[{newSymbol},
SetAttributes[Evaluate[newSymbol = Unique[sym]], Orderless];
Evaluate[newSymbol][cseq] := rhs;
sym[tags] = newSymbol ;]
In[3]:=
makeOrderless[f[7][x___] := Print[x]]
In[4]:=
makeOrderless[f[8][x___] := dontPrint[x]]
In[5]:=
f[7][7, 3, 4, 5, 1]
1\[InvisibleSpace]3\[InvisibleSpace]4\[InvisibleSpace]5\[InvisibleSpace]7
In[6]:=
f[8][7, 3, 4, 5, 1]
Out[6]=
dontPrint[1, 3, 4, 5, 7]
In[7]:=
?f
"Global`f"
\!\(\*
InterpretationBox[GridBox[{
{GridBox[{
{\(f[7] = f$5\)},
{" "},
{\(f[8] = f$6\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnWidths->0.999,
ColumnAlignments->{Left}]}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
Definition[ f],
Editable->False]\)
In[8]:=
?f$*
f$5 f$6
In[9]:=
?f$5
"Global`f$5"
\!\(\*
InterpretationBox[GridBox[{
{\(Attributes[f$5] = {Orderless}\)},
{" "},
{GridBox[{
{\(f$5[x___] := Print[x]\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnWidths->0.999,
ColumnAlignments->{Left}]}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
Definition[ f$5],
Editable->False]\)
In[10]:=
?f$6
"Global`f$6"
\!\(\*
InterpretationBox[GridBox[{
{\(Attributes[f$6] = {Orderless}\)},
{" "},
{GridBox[{
{\(f$6[x___] := dontPrint[x]\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnWidths->0.999,
ColumnAlignments->{Left}]}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
Definition[ f$6],
Editable->False]\)
In[11]:=
DownValues[f]
Out[11]=
{HoldPattern[f[7]] :> f$5, HoldPattern[f[8]] :> f$6}
(2) another idea
If you have already defined your functions, or if you do not want to
attach definitions to them (e.g. you use them only as containers or as a
datatype) then just define
In[12]:=
g[5][x___] /; ! OrderedQ[{x}] := Apply[g[5], Sort[{x}]]
In[13]:=
g[5][7, 3, 4, 5, 1]
Out[13]=
g[5][1, 3, 4, 5, 7]
Kind regards, Hartmut