Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Named Patterns in Switch

  • To: mathgroup at smc.vnet.net
  • Subject: [mg48961] Re: Named Patterns in Switch
  • From: "Peltio" <peltio at twilight.zone>
  • Date: Fri, 25 Jun 2004 02:58:31 -0400 (EDT)
  • References: <cbe8ta$svc$1@smc.vnet.net>
  • Reply-to: "Peltio" <peltioNOSP at Mdespammed.com.invalid>
  • Sender: owner-wri-mathgroup at wolfram.com

"David Park" wrote

>Here is an attempted routine using Switch that does not work.
>
>    foo[expr_] :=
>     Switch[expr,
>            (a_.)*x^(n_), a,
>            (a_.)*y^(n_), n]

>    foo[3*x^3]
>    a (I was hoping for 3)
>
>Switch uses patterns, but any named patterns are useless. So the a in the
third argument in Switch has nothing to do with the a_. in the second
argument.

Sadly True.

>Is there some Mathematica construction that will test successive patterns
with names, do a calculation with the first match and use the names in the
patterns?

A list of rules, maybe.

    foo[expr_] :=
         expr /. {a_.*x^n_ -> a, a_.*y^n_ -> n}

We can stuff the rules into Switch if we like:

    foo[expr_] :=
        Switch[expr,
            a_.*x^n_, expr /. a_.*x^n_ -> a,
            a_.*y^n_, expr /. a_.*y^n_ -> n
        ]

It does not look elegant, but it should do its dirty work nonetheless.

cheers,
Peltio
Invalid address in reply-to. Crafty demunging required to mail me.




  • Prev by Date: Re: Named Patterns in Switch
  • Next by Date: Re: Printing Rotated Text from a Mac
  • Previous by thread: Re: Named Patterns in Switch
  • Next by thread: RE: Named Patterns in Switch