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
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.