MathGroup Archive 2014

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

Search the Archive

Re: Default value in pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg132237] Re: Default value in pattern matching
  • From: Larry Adelston <larrya at wolfram.com>
  • Date: Tue, 21 Jan 2014 03:47:55 -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
  • References: <20140118095946.3B4C36A11@smc.vnet.net>

Don't you need to define myPlus[b_.]?

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

Out[176]= "matched"


Larry

----- Original Message -----
> From: "Yi Wang" <tririverwangyi at gmail.com>
> To: mathgroup at smc.vnet.net
> Sent: Saturday, January 18, 2014 3:59:46 AM
> Subject: Default value in pattern matching
> 
> 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: Re: Unnecessary parentheses around TagBox for MakeBoxes?
  • Next by Date: Re: Why does _+_==2_ (or, why is HoldPattern required
  • Previous by thread: Default value in pattern matching
  • Next by thread: Re: Default value in pattern matching