Fun with lists



I think of two functions that may already exist :

 cyclically permute the elements of a list
cycle([a,b,c,d,e,f,g],2) -->  [c,d,e,f,g,a,b]


kind of discrete integration :
 transforming [a1,a2,a3,...,an] into [a1,a1+a2,a1+a2+a3, ... ,a1+a2+...+an]
(may be with other operators than +)

Eric Reyssat

Jaime Villate a ?crit :
> 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
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
>