Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The answer to this might be simple, but why would ReplaceAll return such a weird result when replacing a number in a list of numbers:

{1, 2, 3, 4}/.2 -> 5
(* {5., 10., 15., 20.} -> 5 *)

even though it does fine with this?

{1, 2, 3, 4} /. x_ /; x == 2 -> 5
(* {1, 5, 3, 4} *)

As far as I know, 2 after /. represents a pattern that matches the number 2. For example, these both work:

Cases[{1, 2, 3, 4}, 2]
(* {2} *)

Position[{1, 2, 3, 4}, 2]
(* {{2}} *)

All the examples of ReplaceAll in the documentation seem to replace only symbols, and I couldn't find a similar questions on SE, hence this question.

share|improve this question

closed as off-topic by Öskå, RunnyKine, Michael E2, ubpdqn, Mr.Wizard Jul 21 at 6:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Öskå, RunnyKine, Michael E2, ubpdqn, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

    
These tools can help you avoid or diagnose such problems in the future: (3146) –  Mr.Wizard Jul 21 at 6:54
    
Also, familiarize yourself with the operator precedence table. Note that "forms representing numbers" are listed first which means they have the highest binding power, which explains why /.2 is /(.2) rather than /.(2). –  Mr.Wizard Jul 21 at 6:57
    
Thanks so much @Mr.Wizard! These resources you gave me are very useful. –  seismatica Jul 21 at 7:00

2 Answers 2

up vote 8 down vote accepted

This is a common problem when using /. with numeric LHS for the replacement. Since you didn't include a space after /., Mathematica parses it as a division by 0.2. Check the full form to convince yourself:

FullForm@Hold[{1,2,3,4}/.2->5]
(* Hold[Rule[Times[List[1,2,3,4],Power[0.2`,-1]],5]] *)

It is generally helpful to leave a space on either side of infix operators to improve clarity and avoid pitfalls like this one.

share|improve this answer
    
Thanks @rm -rf! –  seismatica Jul 21 at 6:25

... because you should write

{1, 2, 3, 4} /. (2) -> 5
share|improve this answer
    
oh doh! I'm so dumb sometimes. –  seismatica Jul 20 at 21:06
1  
I prefer {1, 2, 3, 4} /. {2 -> 5}. –  celtschk Jul 21 at 8:59
    
eldo, you deleted your Riffle question. Are you planning to resurrect that (clarified, hopefully)? Do you have your solution? –  Mr.Wizard Jul 22 at 3:04
    
@Mr.Wizard I'm just trying to delete my account with SE, but they make it very complicated. –  eldo Jul 22 at 3:07
    
@eldo Please, why would you want to delete your account with Stack Exchange? Your participation is appreciated! –  Mr.Wizard Jul 22 at 3:17

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