Re: ReplacePart except first element?
- To: mathgroup at smc.vnet.net
- Subject: [mg102088] Re: [mg102053] ReplacePart except first element?
- From: Tomas Garza <tgarza10 at msn.com>
- Date: Wed, 29 Jul 2009 05:09:15 -0400 (EDT)
- References: <200907280603.CAA14703@smc.vnet.net>
Your definition is OK. I see nothing wrong with it:
In[26]:= h[x_List,a_Integer]:=ReplacePart[x,a->x[[a]]+1]
In[27]:= h[{1,2,3},2]
Out[27]= {1,3,3}
In[28]:= h[{1,2,3},1]
Out[28]= {2,2,3}
Tomas
> Date: Tue, 28 Jul 2009 02:03:24 -0400
> From: alexandrepassosalmeida at gmail.com
> Subject: [mg102053] ReplacePart except first element?
> To: mathgroup at smc.vnet.net
>
> 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?
>
>
>
- References:
- ReplacePart except first element?
- From: mokambo <alexandrepassosalmeida@gmail.com>
- ReplacePart except first element?