Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1993
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1993

[Date Index] [Thread Index] [Author Index]

Search the Archive

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






  • Prev by Date: Re: Bug in ANIMATION???
  • Next by Date: Re: Lists
  • Previous by thread: Lists
  • Next by thread: Re: Lists