question about sublist



On Tue, 2007-04-17 at 02:42 -0400, sen1 at math.msu.edu wrote:
>   I have a large list of floating point numbers.
> 
> Does anyone have a clever predicate to sublist which will extract the
>   elements whose immediate successors have the same sign? 
here is an idea:

(%i1) sublist(x):= block([l: [], prev: x[1]], for i:2 thru length(x) do
        (if prev*x[i] > 0 then l: cons(prev,l), prev:x[i]), reverse(l));
(%i2) a: [2,3,-3,-2,-1, 0, 5, 7]$
(%i3) sublist(a);
(%o5) [2, - 3, - 2, 5]

The values for the sublist are prepended to the new list and at the end
the list is reversed, because in my experience with very large lists
that's much faster than using endcons to append to the new list.
You would have to decide what to do with zero values and with the last
element in the list. The function should also check that the argument
is a list with at least two elements.

Regards,
Jaime