Message Boards Message Boards

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

Rules to act on rules

Posted 9 years ago

If I have a list of rules such as this,

rules = {a -> tt, b -> ff, c -> tt, d -> ff} ,

then what is the correct code to replace the rule c->tt in the list with a new rule, like c->ff?

POSTED BY: Felix K
7 Replies

You can also take the associations based approach:

 Normal@<|rules, c -> ff|>
{a -> tt, b -> ff, c -> ff, d -> ff}
POSTED BY: Kuba Podkalicki
Posted 9 years ago

Thanks. This one is surely correct.

POSTED BY: Felix K
Posted 9 years ago

Hi, maybe "The Standard Evaluation Procedure" reference is helpful for you. Wolfram usually starts from the head of the function to read attributes. Afterwards the default evaluation process starts from the innermost subexpression(leaves). But here you dont have an evaluation but a rule .You could write it as

rules = ReplaceAll[rules, (c -> tt) -> (c -> ff)]

and it just replaces the part "c->tt" by "c->ff" and gives the final result. If you would try

rules = ReplaceAll[rules, c -> ff]

just the "c" would be replaced by "ff" and you would obtain

{a -> tt, b -> ff, ff -> tt, d -> ff}

The question regarding your TreeForm might be answered by the example

ReplaceAll[Sin[Pi],Pi->a]

which gives just 0 as an output, since here the Trace[] is, that first Sin[Pi] is evaluated and thus the Pi vanished and cant be replaced anymore. If you want to have a different sequence of evaluations than innermost subexpression you might use Attributes like HoldAll , to change the behaviour to "Non?Standard Evaluation",

POSTED BY: Till Luc Lange
Posted 9 years ago

Thank you very much! So that ought to be the right way of doing it. You showed me where to look in the reference.

POSTED BY: Felix K
Posted 9 years ago

The use of parentheses makes (c -> tt) the first item to be evaluated, so why does the code not give, for instance, {a -> tt, b -> ff, tt -> ff, d -> ff} instead? Yes, the expression has the following tree form, but what in the Wolfram language determines the sequence of operations in this case?

tree form

POSTED BY: Felix K
Posted 9 years ago

Perhaps

In[1]:= rules = {a -> tt, b -> ff, c -> tt, d -> ff};
rules = rules /. (c -> tt) -> (c -> ff)

Out[2]= {a -> tt, b -> ff, c -> ff, d -> ff}
POSTED BY: Bill Simpson
Posted 9 years ago

Thanks for the reply. I guessed that method too, but am not sure whether it is completely justifiable.

POSTED BY: Felix K
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