ReplacePart except first element?
- To: mathgroup at smc.vnet.net
- Subject: [mg102053] ReplacePart except first element?
- From: mokambo <alexandrepassosalmeida at gmail.com>
- 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?
- Follow-Ups:
- Re: ReplacePart except first element?
- From: Tomas Garza <tgarza10@msn.com>
- Re: ReplacePart except first element?
- From: "Elton Kurt TeKolste" <tekolste@fastmail.us>
- Re: ReplacePart except first element?
- From: Patrick Scheibe <pscheibe@trm.uni-leipzig.de>
- Re: ReplacePart except first element?