Re: List element manipulation

• To: mathgroup at smc.vnet.net
• Subject: [mg25437] Re: [mg25408] List element manipulation
• From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
• Date: Sun, 1 Oct 2000 02:44:21 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```In fact it is indeed pretty easy, e.g.:

In[10]:=
MapAt[# - 1 &, Range[116, 166, 8], -1]

Out[10]=
{116, 124, 132, 140, 148, 156, 163}

Decrement does not work because

In[11]:=?Decrement
x-- decreases the value of x by 1, returning the old value of x.

ReplacePart should work except that you are again trying to use -- in an
impermissable way. It only works on variables! Compare;

In[12]:=
x = 3;

In[13]:=
--x

Out[13]=
2

In[14]:=
--3

PreDecrement::rvalue:
3 is not a variable with a value, so its value cannot be
changed.

Out[14]=
--3

The correct versions of yur attempts are here:

In[15]:=
Range[116, 166, 8] // ReplacePart[#, Last[#] - 1, -1] &

Out[15]=
{116, 124, 132, 140, 148, 156, 163}

In[15]:=
Range[116, 166, 8] // ReplacePart[#, #[[-1]] - 1, -1] &

Out[16]=
{116, 124, 132, 140, 148, 156, 163}

--
Andrzej Kozlowski
Toyama International University
JAPAN

http://platon.c.u-tokyo.ac.jp/andrzej/
http://sigma.tuins.ac.jp/

on 00.9.29 2:06 PM, Martin Rommel at Martin_Rommel at mac.com wrote:

> I want to decrement the last number in list. This ought to be easy, but my
> initial attempts did not bear fruits:
>
> In[40]:=
> MapAt[Decrement, Range[116, 166, 8], -1]
>
> Decrement::"rvalue": "164 is not a variable with a value, so its value
> cannot be changed."
>
> Out[40]=
> {116, 124, 132, 140, 148, 156, 164--}
>
> Do I need to use ReplacePart? That works but still gives me an error!
>
> In[53]:=
> Range[116, 166, 8] // ReplacePart[#, --Last[#], -1] &
>
> Set::"write": "Tag Last in Last[{116, 124, 132, 140, 148, 156, 164}] is
> Protected."
>
> Out[53]=
> {116, 124, 132, 140, 148, 156, 163}
>
> Different error here:
>
> In[51]:=
> Range[116, 166, 8] // ReplacePart[#, --#[[-1]], -1] &
>
> Set::"setps": "{116, 124, 132, 140, 148, 156, 164} in assignment of part
> is not a symbol."
>
> Out[51]=
> {116, 124, 132, 140, 148, 156, 163}
>
>
> Anybody out there able to enlighten me?
>
> Thanks, Martin
>
>
>

```

• Prev by Date: NDSolve with a function which calls N
• Next by Date: Re: Another strange bug in Mathematica 4.0's Integrate
• Previous by thread: Re: List element manipulation
• Next by thread: RE: List element manipulation