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.

I access the first part of an expression with head foo without a problem:

foo[arg][[1]]

arg

Then when I try to use this in a replacement rule, it works correctly, but an error spits out

foo[arg] /. { x_foo -> bar[ z, x[[1]] ] }

Part::partd: Part specification x[[1]] is longer than depth of object. >>

bar[z, arg]

Why does Mathematica think there's a problem here?

share|improve this question

closed as off-topic by Kuba, rasher, ubpdqn, Yves Klett, Michael E2 Apr 28 at 10:43

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." – Kuba, rasher, ubpdqn, Yves Klett, Michael E2
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
Rule\Details: lhs->rhs evaluates rhs immediately., use RuleDelayed (:>) –  Kuba Dec 9 '13 at 5:53
2  
Kuba's comment answers your question directly, but you might consider a different approach that doesn't use Part and thus sidesteps the issue of Rule vs. RuleDelayed: foo[arg] /. {foo[x_] -> bar[z, x]} –  m_goldberg Dec 9 '13 at 7:14

Browse other questions tagged or ask your own question.