question about sublist



On 4/17/07, Jaime E. Villate <villate at fe.up.pt> wrote:
>
> (%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));


This will work, but using indexing to access elements of a Maxima list is
not very efficient; each index operation x[i] takes O(i) time.

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.


Yes, endcons takes O(length(list)).

              -s