[Date Index] [Thread Index] [Author Index]
Re: pattern matching: rules that stop other rules?
Hi, consider: Attributes[If] and you will see that If has the attribute HoldRest. This means that only the first argument is evaluated before feeding it to "If", the other arguments are NOT evaluated. This leads to the behaviour, that only one of the if/else branches is actually executed. If you have e.g. side effects, you would not like to have a different behaviour. Daniel croddie at princeton.edu wrote: > 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. >