Re: ReplacePart except first element?

• To: mathgroup at smc.vnet.net
• Subject: [mg102093] Re: [mg102053] ReplacePart except first element?
• From: "Elton Kurt TeKolste" <tekolste at fastmail.us>
• Date: Wed, 29 Jul 2009 05:10:13 -0400 (EDT)
• References: <200907280603.CAA14703@smc.vnet.net>

```Exactly: ReplacePart has no side effects.  All you need to do is to
replace x by the result:

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

In[188]:= x = {1, 2, 3}

Out[188]= {1, 2, 3}

In[189]:= x = h[x, 2]

Out[189]= {1, 3, 3}

In[190]:= x

Out[190]= {1, 3, 3}

(Note that I did not see the problem that you did with dropping the
first element...)

On Tue, 28 Jul 2009 02:03 -0400, "mokambo"
<alexandrepassosalmeida at gmail.com> wrote:
> 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 to group the graph
• Next by Date: Re: ReplacePart except first element?
• Previous by thread: Re: ReplacePart except first element?
• Next by thread: Re: ReplacePart except first element?