{"test", 3} /. s_String :> StringReverse[s]
{"test", 3} /. s_String -> StringReverse[s]
The second line gives the error:
StringReverse: String expected at position 1 in StringReverse[s]
Question: why does it give the error with Rule, but not RuleDelayed?
EDIT: I noticed that {3, 4} /. s_ -> Sin[s] // N
works without error. What is the difference?
StringReverse[s]
on its own, withouts
having a value. $\endgroup$Set
andRule
. In fact the LHS is evaluated first, as you assumed. But LHS of the rule is onlys_String
. The/.
and what comes to the left of that is not part of the rule. It's the reverse: The rule is part of the/.
. Look at the full form:ReplaceAll[{"test", 3}, Rule[s_String, StringReverse[s]]]
. $\endgroup$Rule
norReplaceAll
have any (relevant)Hold*
attributes, the standard evaluation sequence is followed: 1. left to right, starting with the head and continuing with arguments. 2. then apply definitions associated with the head. This means thatStringReverse[s]
gets evaluated beforeReplaceAll
has a chance to do anything with it. $\endgroup${3, 4} /. x_ -> Sin[x] // N
work without error then? HereSin[x]
is evaluated before Mathematica knowsx
is a real number... $\endgroup$