Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2014

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

Search the Archive

Default value in pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg132208] Default value in pattern matching
  • From: Yi Wang <tririverwangyi at gmail.com>
  • Date: Sat, 18 Jan 2014 04:59:46 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net

Hello,

I was investigating the usage of the "a_." pattern. I understand that for some built-in functions with default value, we can do

In[] := a /. {a + b_. -> "matched"}

Out[] = "matched"

However, if I define a function myself with 

In[] := Default[myPlus] = 0;
        myPlus[0] := 0;
        a /. {a + myPlus[b_.] -> "matched"}

Out[] = a

In other words, my default value is not used in the "b_." pattern. Is there a way to get a /. {a + myPlus[b_.] -> "matched"} match in the same way as a /. {a + b_. -> "matched"}?

Otherwise I have to write separately as

In[] := a /. {a -> "matched", a + myPlus[b_] -> "matched"}

This is a lot of additional work for complicated cases.



  • Prev by Date: Format expression in TeXForm not working
  • Next by Date: compensated summation in norm
  • Previous by thread: Format expression in TeXForm not working
  • Next by thread: Re: Default value in pattern matching