Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Pattern matching
Replies: 5   Last Post: Feb 21, 2011 4:20 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Charles Gillingham

Posts: 14
Registered: 10/30/10
Re: Pattern matching
Posted: Feb 19, 2011 5:13 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

There is no way to modify the rule and get the result you expect. f[u+0] becomes f[u] before the rule is even applied. You can modify the way you apply the rule, like so:

rule = f[u + x_ ] :> x

ReleaseHold[ Hold[ f[u+0] ] /. rule ]



Begin forwarded message:

From: "Andr=E9 M. Grabinski"@smc.vnet.net
Date: February 18, 2011 1:33:47 AM
Subject: Pattern matching

Hi everyone,

hopefully someone of you could answer me a simple question concerning
patterns in Mathematica 8. Consider the following minimal example,


In[1]:= rule = f[u + x_] -> x;
f[u + 5] /. rule
f[u + 0] /. rule
f[u] /. rule

Out[1]= 5

Out[2]= f[u]

Out[3]= f[u]


Now the question: in which way do I need to modify "rule" in order to
get "0" as result for "Out[3]"? Of course I could add a second rule,
i.e. "rule = {f[u + x_] -> x,f[u] -> 0}", but I feel like there should
be a more elegant way than to take care of each and every exception
(okay, in this minimal example this wouldn't matter but I'm just
interested in the general way of setting up patterns).

Thank you very much in advance,

A.G.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.