MathGroup Archive 2009

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

Search the Archive

ReplacePart except first element?

  • To: mathgroup at
  • Subject: [mg102053] ReplacePart except first element?
  • From: mokambo <alexandrepassosalmeida at>
  • Date: Tue, 28 Jul 2009 02:03:24 -0400 (EDT)

I was trying to modify a list by adding 1 to a certain element.
I can't understand the following result:

h[x_List, a_Integer] := ReplacePart[x, a -> x[[a]] + 1]

h[{1, 2, 3}, 2] gives {,3,3} (what happened to the first element?) but
h[{1, 2, 3}, 1] gives {2,2,3} as expected.

Eventually I changed to:

h[x_List, a_Integer] := MapAt[Function[y, y + 1], x, a]

but the ReplacePart version still puzzles me.
Is it because ReplacePart creates a copy instead of altering inline
like x[[a]] = val does?

  • Prev by Date: Re: How do you take apart a sequence?
  • Next by Date: mimic InterpolatingFunction et. al. formatting
  • Previous by thread: Re: How to group the graph
  • Next by thread: Re: ReplacePart except first element?