> Does someone have a library roughly equivalent
> to some of the the Mma list functions ?
> Take,Partition,Split,...
Such a library is useful because I am translating/recoding
some Mma code, and sometimes it would be easier to translate
it more directly.
Looking at the archives, I don't see much talk about it.
Anyway, here are four Mma functions that I needed. I think its
worth posting these somewhere. Of course they should have
some error checking, etc., added.
/* list_partition. Similar to Mma's Partition
but this treats overhanging differently.
list_partition(x,n) partitions list x into a n
parts (each the element of a list)
list_partition(x,n,d) does the same with offset d
list_partition([a,b,c,d,e,f],2);
--> [[a,b],[c,d],[e,f]]
list_partition([a,b,c,d,e,f],2,1);
--> [[a,b],[b,c],[c,d],[d,e],[e,f]]
*/
list_partition(x,n,[id]) := block([d:0],
if length(id) = 1 then d : id[1],
if length(x) <= n then [x] else
cons(rest(x,n-length(x)),list_partition(rest(x,n-d),n,d)));
/* fold. Same as Mma Fold, ie,
fold(f,x,[a,b,c]) --> f(f(f(x,a),b),c)
*/
fold(f%,x,v) := if length(v)=1 then apply(f%,[x,v[1]])
else apply(f%,[fold(f%,x,rest(v,-1)),last(v)]);
/* foldlist. same as Mma FoldList, ie,
foldlist(f,x,[a,b,c]) --> [x, f(x,a), f(f(x,a),b), f(f(f(x,a),b),c)]
*/
foldlist(f%,x,v) := block( [e],
if length(v)=0 then [x]
else ( e : foldlist(f%,x,rest(v,-1)),
endcons(apply(f%,[last(e),last(v)]),e)));
/* nest. Same as Mma Nest
ie nest(f,x,3) --> f(f(f(x)))
*/
nest(f%,x,n) := if n=0 then x
else if n=1 then apply(f%,[x])
else apply(f%,[nest(f%,x,n-1)]);