{"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, withoutshaving a value. – Szabolcs Aug 19 '17 at 10:16SetandRule. 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]]]. – Szabolcs Aug 19 '17 at 11:06RulenorReplaceAllhave 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 beforeReplaceAllhas a chance to do anything with it. – Szabolcs Aug 19 '17 at 11:09{3, 4} /. x_ -> Sin[x] // Nwork without error then? HereSin[x]is evaluated before Mathematica knowsxis a real number... – GambitSquared Aug 19 '17 at 18:03Sin[x]on its own ... Is there an error? Do the same withStringReverse[x]now. Is there an error? – Szabolcs Aug 20 '17 at 06:53