MathGroup Archive 1999

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

Search the Archive

Re: Pattern example

  • To: mathgroup at smc.vnet.net
  • Subject: [mg19408] Re: [mg19284] Pattern example
  • From: "David Park" <djmp at earthlink.net>
  • Date: Mon, 23 Aug 1999 13:57:19 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

>I am trying to learn how to use patterns and replacements,
>for instance this example:
>
>{{1+a},{2+a},{3+a}}  /.  {t_+a}->c
> {c,c,c}
>
>FullForm[{{1+a},{2+a},{3+a}}  /.  {t_+a}->c]
>FullForm= List[c,c,c]
>
>So far so good
>
>Then
>{{1+a},{2+a},{3+a}}  /.  t_->c
>c
> this is no more a list, but

In the last case the whole expression matched. t_ will match List[__] or any
expression. Incidentally, you could leave the t out and just use _ -> c.

>
>FullForm[{{1+a},{2+a},{3+a}}  /.  {t_}->c]
>List[c,c,c]
>shows it as a list

But you didn't use the same rule. Now each sublist matches, but the whole list does
not match because the whole list has three, not one, arguments. If you had used
{t__}->c in the above example you would again have gotten the single c, because now
the pattern again matches a list with one or more arguments. Again, you could have
left the t out.

It seems to take a lot of practice to master pattern matching. I wish I had it
mastered! It would be more informative for you to use FullForm on the expression you
are using the pattern on, and not the result after the rule is applied. This often
show why a pattern matches, or more often doesn't match.

>
>What is behind this difference?
>
>Chris
>
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/





  • Prev by Date: Re: strange problem with tickmarks
  • Next by Date: Re: Simulation
  • Previous by thread: Re: Pattern example
  • Next by thread: Re: Simple? 3D Plotting Problems