0

The first example on the page for SequenceReplace is the following:

SequenceReplace[{a, b, x, x, a, c}, {a, e_} :> e]

(*b,x,x,c*)

Why do they use RuleDelayed here, instead of ->? I tried it with -> and got the same answer.

user106860
  • 829
  • 4
  • 10
  • 2
    In general, when working with patterns, it's just safer. For ->, e is only bound by e_ if it is present in the rhs after evaluation. So for example, e = 1; SequenceReplace[{a, b, x, x, a, c}, {a, e_} -> e] gets you {1, x, x, 1}. So it's good practice to use :> just in case e is defined somewhere else, I think. – thorimur Sep 05 '21 at 19:24

1 Answers1

2

I remember this was asked before, but can't find it now. i.e. when to use :> vs ->. As @thorimur mentions, it is related to when right side is evaluated.

If you have Wagner book, look at page 148, he gives a nice example showing when to use and what problem it can cause if you use -> instead of :>

enter image description here enter image description here

This book is now free to download. Thanks to this post Are you interested in purchasing David Wagner's "Power programming with Mathematica"?

enter image description here

Nasser
  • 143,286
  • 11
  • 154
  • 359