Filtering Lists



Stavros Macrakis wrote:
> On 10/26/07, S. Newhouse <sen1 at math.msu.edu> wrote:
>   
>> Why does the 'non-evaluation' of f in this application of sublist work?
>> It seems that both
>>    sublist([1,2,3,4,5],'f);
>> and
>>    sublist([1,2,3,4,5],f);
>> work
>>
>> Why does this happen?
>>
>> Since, (I think) sublist finds those terms in a list for which a
>> predicate is true,  why does it work if the predicate is not evaluated?
>>     
>
> Sublist is a normal function; the Maxima evaluator evaluates and
> simplifies its arguments.  Thus
>
>    myfun : evenp;
>    sublist(append([1,2,3],[2,5]), myfun);
>
> is equivalent to
>
>    sublist( '[1,2,3,2,5], 'evenp );
>
> In both cases, the sublist function (or functional if you prefer that
> terminology) then applies the predicate, a functional argument, to
> each element of the list.  The application of the predicate is
> independent of where the predicate came from (a quoted argument or an
> evaluated argument).
>
> Quoting the functional argument prevents this level of evaluation from
> happening:
>
>    sublist( '[1,2,3], 'myfun )
>
> will apply the literal function myfun to each of 1,2,3.  Sublist
> treats any non-false value as true (I think this is a design error,
> but that's a different discussion) so this will return [1,2,3].
>
> Is that clearer?
>
>           -s
> .
>
>   
Here is what I get.
(%i21) myfun: evenp;
(%o21)                               evenp
(%i22) sublist('[1,2,3],'myfun);
(%o22)                                [2]
(%i23) sublist('[1,2,3],myfun);
(%o23)                                [2]
(%i24) sublist([1,2,3],'myfun);
(%o24)                                [2]
(%i25) sublist([1,2,3],myfun);
(%o25)                                [2]
(%i26) xx: [1,2,3];
(%o26)                             [1, 2, 3]
(%i27) sublist(xx,myfun);
(%o27)                                [2]
(%i28) sublist(xx,'myfun);
(%o28)                                [2]
(%i29) sublist('xx,'myfun);

The first argument to `sublist' must be a list:
xx
 -- an error.  To debug this try debugmode(true);
(%i30) build_info();

Maxima version: 5.13.0
Maxima build date: 21:33 9/7/2007
host type: i686-pc-linux-gnu
lisp-implementation-type: SBCL
lisp-implementation-version: 1.0.7

I guess one just has to try various examples to see how to apply these 
things.

-sen