Fun with lists



Hi,
these are very trivial list functions that can be very handy. Please
comment on more appropriate names, better ways to do the same, other
similar functions that you would like to see in a package of list
utilities, etc.

Regards,
Jaime

(%i1) list_slice(l,n,m):=if (n>0) then rest(rest(l,n+m-1-length(l)),n-1)
  else rest(rest(l,m+n-1),length(l)+n)$

(%i2) list_remove(l,n,m):=if listp(l)
  then append(rest(l,n-1-length(l)),rest(l,n+m-1))
  else funmake(op(l),list_remove(args(l),n,m))$

(%i3) list_shuffle(l) := block([r], for n:length(l) step -1 thru 2
 do (r:random(n), l:endcons(part(l,r+1),list_remove(l,r+1,1))), l)$

(%i4) list_slice([a,b,c,d,e], 2, 3);
(%o4)                    [b, c, d]

(%i5) list_remove([a,b,c,d,e], 2, 3);
(%o5)                      [a, e]

(%i6) list_shuffle([a,b,c,d,e]);
(%o6)                 [a, c, d, e, b]

(%i9) list_shuffle(a.b.c.d.e);
(%o9)                c . a . d . e . b