RE: pattern matching: rules that stop other rules?

*To*: mathgroup at smc.vnet.net*Subject*: [mg71633] RE: [mg71596] pattern matching: rules that stop other rules?*From*: "David Park" <djmp at earthlink.net>*Date*: Sat, 25 Nov 2006 05:37:14 -0500 (EST)

First, if you use Mathematica in its standard default mode, spaces are automatically inserted where appropriate and adding additional space usually does little for readability except between larger chuncks of input. f[x_] := 0 Next, check the attributes of If. Attributes[If] {HoldRest, Protected} So the If statement will not evaluate f[2], at least not until it determines that p is False. If you want f[2] to be evaluated you can specifically Evaluate it. If[p, 1, Evaluate@f[2]] If[p, 1, 0] A replacement rule will replace within held expressions. If you use your own function instead of If, then none of the arguments are held, unless you give your function one of the held attributes, and then all arguments are automatically evaluated. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: croddie at princeton.edu [mailto:croddie at princeton.edu] Hello. I've been using Mathamatica for quite a while but without ever finding out how the language works fundamentally, which I'm trying to do now. I'd be grateful for some help in understanding patterns - it seems like a powerful idea to me. There is something I can't work out. Define a function f [x_]:=0 say Now If [ p, 1, f [ 2] ] evaluates to itself. So the rule in the definition is not applied to f [ 2 ]. Replace If with some other undefined function, say qwerty, and you get qwerty [ p, 1, 0] not surprisingly. And If [ p, 1, f [ 2] ] /. f [x_]->0 returns If [ p, 1, 0 ]. Is there a rule associated with If that stops a rule (if that's the right expression) from being applied inside it? Can users write such rules? Thanks for any help.