Re: Lists
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Lists
- From: John Lee <lee at math.washington.edu>
- Date: Fri, 29 Jan 93 12:48:49 -0800
neilb at physics.su.oz.au (RiemannZeta(s)) write:
> >In[5]:= l={x,z,y,0,0}
> >
> >Out[5]= {x, z, y, 0, 0}
> >
> >In[6]:= l/.l[[Length[l] ]]->l[[Length[l] ]]+1
> >
> >Out[6]= {x, z, y, 1, 1}
> >
> which is curious. Does anybody know why
> this doesn't produce
> {x,y,z,0,1}?
Since the construct l/.b->c evaluates b and c before applying the rule, and
since l[[ Length[l] ]] has the value 0, your expression In[6] is equivalent
to
l /. 0 -> 1
This explains why you got the result you did. One function you might use
here is AddTo (which can be abbreviated +=). For example:
In[4]:= l={x,z,y,0,0}
Out[4]= {x, z, y, 0, 0}
In[5]:= l[[ Length[l]] ] += 1
Out[5]= 1
In[6]:= l
Out[6]= {x, z, y, 0, 1}
If you're always adding exactly 1, you might also look at the functions
Increment and PreIncrement.
Jack Lee
Dept. of Mathematics
University of Washington
Seattle, WA