DMUG-Archiv 2008

Frühere   Chronologischer Index   Spätere
Vorherige   Thematischer Index   Nächste

Re: scoping Rule[]

Hallo Robert,

in der mma hilfe steht das patterns als lokale symbole behandelt werden:

tutorial/VariablesInPureFunctionsAndRules
"
Mathematica has several "scoping constructs" in which certain names are treated as local. When you mix these constructs in any way, Mathematica does appropriate renamings to avoid conflicts.
"

kurz darauf steht im selben Tutorial (nach Out[17])

When you apply a rule such as f[x_]->rhs, or use adefinition such as f[x_]:=rhs, Mathematica implicitlyhas to substitute for x everywhere in the expression rhs.It effectively does this using the /. operator. As a result,such substitution does not respect scoping constructs.However, when the insides of a scoping construct are modifiedby the substitution, the other variables in the scoping constructare renamed.

Wenn man die Schriftfarben auf einem Farbdisplay anschaut,
dann sieht man schon beim Tippen, dass gleich etwas
nichttriviales passieren wird:

In[1]:= x = z;
(* x ist schwarz, z ist blau *)
(*2*) {a, b} /. {x_, y_} :> {y, x}
(* a, b blau, x_, y_, x, y grün *)
(*3*) {a, b} /. {x_, y_} -> {y, x}
(* a, b, y blau, x_, y_ grün, x schwarz (!) *)

Das Syntax Coloring (Edit->Preferences->Appearance) färbt
lokale Variable, Funktionsargumente und Patternnamen
grün (und Face italic), unbekannte Grössen blau, etwa

In[2]:= Fatzkerade[]

wird blau und NumberQ schwarz:

In[3]:= NumberQ[2]

Das x erscheint bei Rule[] schwarz, das y blau,
der Parser betrachtet diese beiden, x und y, schon
nicht als Inkarnation der Platzhalter des Patterns:
Es findet eine freie Ersetzung statt, nicht ein Vertauschen.
Für das Vertauschen braucht Mma ein RuleDelayed[] weil
dann die rechte Seite erst evaluiert wird, wenn die
Regel benutzt wird, d.h. nachdem die linke Seite gefunden wurde.

Die anderen Beispiele sind nicht verwunderlich wegen

In[31]:= x
Out[31]= z

Gruss
Udo.



Antworten:
Verweise:
Frühere   Chronologischer Index   Spätere
Vorherige   Thematischer Index   Nächste

DMUG DMUG-Archiv, http://www.mathematica.ch/archiv.html