MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: negative pattern matching anyone?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg43723] Re: negative pattern matching anyone?
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Thu, 2 Oct 2003 02:51:18 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <blcqqj$p8h$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <blcqqj$p8h$1 at smc.vnet.net>,
 Paolo Bientinesi <pauldj at cs.utexas.edu> wrote:

> Hello,
> could anyone explain me why -3 is different from -a in the
> following situation, please?
> 
> h[-x_] := Abs[x]
> h[x_]  := x^2
> 
> now, 
> 
> k[-3] = 9
> k[-a] = Abs[a]
> 
> I guess the answer con be found in the FullForm:
> 
> FullForm[-a] = Times[-1,a]
> FullForm[-x_] = Times[-1,Pattern[x,Blank[]]]
> 
> while FullForm[-3] = -3
> 
> 
> 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

-- 
Paul Abbott                                   Phone: +61 8 9380 2734
School of Physics, M013                         Fax: +61 8 9380 1014
The University of Western Australia      (CRICOS Provider No 00126G)         
35 Stirling Highway
Crawley WA 6009                      mailto:paul at physics.uwa.edu.au 
AUSTRALIA                            http://physics.uwa.edu.au/~paul


  • Prev by Date: Non-string output in for loop
  • Next by Date: Re: Polynomial constructing
  • Previous by thread: Re: negative pattern matching anyone?
  • Next by thread: Re: negative pattern matching anyone?