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

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


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

  • 2
    $\begingroup$ 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. $\endgroup$
    – thorimur
    Sep 5 '21 at 19:24

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.