question about sublist



You can do some extra checking, but I think something like this will work:

Extract(input):=block([result:[]],
map(lambda([x,y], if x*y>0 then result:cons(x,result)),  input,
rest(input)),
reverse(result));
 
There may be a macro for   r:cons(x,r)   as  push(x,r). 

RJF

> -----Original Message-----
> From: maxima-bounces at math.utexas.edu 
> [mailto:maxima-bounces at math.utexas.edu] On Behalf Of Jaime E. Villate
> Sent: Tuesday, April 17, 2007 7:10 AM
> To: sen1 at math.msu.edu
> Cc: maxima at math.utexas.edu
> Subject: Re: [Maxima] 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
>   
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>