MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Replacing Parts

  • To: mathgroup at smc.vnet.net
  • Subject: [mg7643] Replacing Parts
  • From: Richard Finley <trfin at fiona.umsmed.edu>
  • Date: Tue, 24 Jun 1997 03:36:06 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

In an earlier communication in response to Robert Prus about problems with
MatrixExp, I wrote about some difficulties I had with replacing parts of a
list or array using the replacement -> .  I have now had time to play a
little more with it and find that I have been working for a long time with a
misunderstanding about replacing parts so I thought others might have the
same problem and benefit from a discussion:

In essence, if you have a list, say 
list1 = { a, b, c, b, a } 
and you want to form a new list
list2 = { a, 3, c, b, a } 
it might seem reasonable to use
list2 = list1 /. list1[ [ 2 ] ] -> 3
but if you do this you will get
list2 = { a, 3, c, 3, a } 
because the Part command is carried out first and you are actually getting
list2 = list1 /. b -> 3

This is somewhat surprising to me because if you use 
list1 [ [ 2 ] ] = 3
you don't get b = 3 but you actually get list1[ [ 2 ] ] = 3 and therefore
list1 = { a, 3, c, b, a }

so I guess Part is evaluated prior to -> but after = ??  This gets a little
subtle for me so if someone can clarify it better, I am interested.

If you want to carry out the operation I intended,  you must do something like

list2 = ReplacePart[ list1, 3, 2 ]

which gives, as intended, 
list2 = { a, 3, c, b, a }

Even when you think you understand certain commands, the subtleties of
execution can sometimes surprise you so it pays to stay alert!!  

RF

   



  • Prev by Date: Patterns
  • Next by Date: Q:SetCoordinates[system] usage
  • Previous by thread: Patterns
  • Next by thread: Q:SetCoordinates[system] usage