Re: Re: negative pattern matching anyone?
- To: mathgroup at smc.vnet.net
- Subject: [mg43798] Re: [mg43766] Re: negative pattern matching anyone?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Mon, 6 Oct 2003 02:07:50 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On Friday, October 3, 2003, at 03:28 PM, Paolo Bientinesi wrote: >>> Then, is there a way to match both -3 and -a with the same pattern? >> >> Yes -- use Sign: >> >> h[x_] := Abs[-x] /; Sign[x] == -1 >> h[x_] := x^2 >> >> Cheers, >> Paul > > > Thanks for all the answers, but all of them solve the problem > with two different patterns (basically using ||). > > About the Sign suggestion, it doesn't seem to work, since > Sign[-a] yields -Sign[a]: > > h[x_ /; Sign[x] == -1]:= Abs[x] > h[x_]:= x^2 > > h[-3] is 3 > > but > > h[-a] is a^2 > -- > Paolo > > pauldj at cs.utexas.edu paolo.bientinesi at iit.cnr.it > > > Actually, you can match both -3 and -a with a single pattern, though it is a rather "artificial" one: {-3, -a} /. _?(StringMatchQ[ToString[#], "-*"] &) -> b {b,b} Andrzej Kozlowski Yokohama, Japan http://www.mimuw.edu.pl/~akoz/ http://platon.c.u-tokyo.ac.jp/andrzej/