Message Boards Message Boards

0
|
1997 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Question about the RuleDelayed and the definition of delayed

Posted 4 years ago

Hi, I have a question about RuleDelayed and pattern variables. I get the difference between the following two statements.

x = 1;
{1, 2, 3} /. i_Integer -> {x++}
{1, 2, 3} /. i_Integer :> {x++}

But isn't the following statement (technically) also some kind of delayed evaluation, because I'm using the pattern variable i on the right hand side of the rule?

x = 1;
{1, 2, 3} /. i_Integer -> {i, x++}

Thank you!

POSTED BY: Timo Kuchheuser
2 Replies
Posted 4 years ago

Hi Timo,

With Rule the pattern will be evaluated prior to application of the rule, so any definition of i that is in scope will be used rather than the value matching the pattern.

x = 1; i = 1;
{1, 2, 3} /. i_Integer -> {i, x++}
(* {{1, 1}, {1, 1}, {1, 1}} *)
POSTED BY: Rohit Namjoshi

Thank you!

POSTED BY: Timo Kuchheuser
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract