Filtering Lists



k:[];
r:[1,2,3,4,5];
for i in r do if oddp(i) then k:cons(i,k);
reverse(k);

There may be simpler ways, if maxima implemented "push" or for that matter,
"filter".

filter(list,pred):= if list=[] then [] else if apply(pred, [first(list)])
then cons (first(list),filter(rest(list),pred)) else
filter(rest(list),pred);

try

filter(r,oddp);

Your predicate seems to involve the use of member.

 

> -----Original Message-----
> From: maxima-bounces at math.utexas.edu 
> [mailto:maxima-bounces at math.utexas.edu] On Behalf Of Peter Danenberg
> Sent: Friday, October 26, 2007 12:13 AM
> To: maxima at math.utexas.edu
> Subject: Filtering Lists
> 
> I'd like to exclude from a list those sublists containing an element
> that satisfies a given predicate; unfortunately, it appears as though
> I have to convert to a set, subset, then retroconvert to a list:
> 
>     listify(subset(setify(elements),
>         lambda([element], not(some(predicatep, element))))));
> 
> Is there any more elegant way of filtering lists without resorting to,
> say, native Lisp?
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>