Re: case of inconsistent API between Drop and Part?
- To: mathgroup at smc.vnet.net
- Subject: [mg127116] Re: case of inconsistent API between Drop and Part?
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Sun, 1 Jul 2012 02:07:50 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201206300917.FAA01642@smc.vnet.net>
- Reply-to: murray at math.umass.edu
To drop just the mth and nth elements of a list is easy to do if m < n: Drop[lis, {m, n, n - m}] That uses the form of Drop where the 2nd argument includes a step size as its 3rd entry. To drop more than two non-consecutive elements would be a different matter, of course. For that you could use Delete. Thus the Drop expression above could be done as: Delete[lis, {{m}, {n}}] The source of all the inconsistencies you indicate may be historical in part: Drop and Part were introduced in Mathematica version 1 (and subsequently modified); Delete only in version 2; Span (the double semicolon) only in Mathematica 6. On 6/30/12 5:17 AM, Nasser M. Abbasi wrote: > One thing I always liked about Mathematica's functions is > that the API's of its functions all seem to be consistent > (for the most part). > > Here is a case where it is not consistent though to share it. > > (I am trying to make a list of all such cases in my cheat sheet) > > It is between Drop and Part. > > Drop[list,{m,n}] gives list with elements m THROUGH n dropped. > Drop[list,m;;n] gives list with elements m THROUGH n dropped. > > but > > Part[list,{m,n}] gives the list of elements m AND n > Part[list,m;;n] gives the list of elements m THROUGH n > > Example using Drop: > ----------------- > Clear[a,b,c,d,e]; > lis={a,b,c,d,e}; > Drop[lis,2;;4] > > Out[161]= {a,e} > > Drop[lis,{2,4}] > > Out[162]= {a,e} > ---------------------- > > Example using Part: > ------------------ > Part[lis,2;;4] > > Out[163]= {b,c,d} > > Part[lis,{2,4}] > > Out[164]= {b,d} > -------------------- > > What the above also mean, is that using the Drop function, > there is no direct way to tell it to Drop element 2 AND > element 4 in one command. (there are easy ways to do this, > but not using one function call of Drop as is). (one easy > solution is to complement the elements to drop, and use Part > instead, and other ways) > > I do not know if the experts here agree that this is an > inconsistency in the API between Drop and Part, but for me, > (and I am no expert) it seems so. > > http://reference.wolfram.com/mathematica/ref/Part.html > http://reference.wolfram.com/mathematica/ref/Drop.html > --Nasser > -- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305