« Return to Thread: maplist and friends

Re: maplist and friends

by Samer Abdallah :: Rate this Message:

Reply to Author | View in Thread


On 21 Apr 2008, at 03:59, Bartha Attila wrote:

> i'm interested in proposals. can you point me to some resources ?
> the 2nd argument of vmaplist_a is in fact a kind of lambda.
> vmaplist just builds this 'lambda-list' and passes it to vmaplist_a.
>

I've been using something like this - it's based on a replacement  
call-like
predicate that accepts a lambda term as the first argument. An example
of a lambda term would be something like:
        ( \(X,Y,Z) :- foo(Z,a), bar(b,X-Y) )
ie, it's a binary term Head :- Body, where Head is an n-ary term
with '\' as the functor, and Body is a goal. It behaves more or less as
if a dummy predicate had been defined
        dummy(X,Y,Z) :-
                foo(Z,a),
                bar(b,X-Y).

I say 'more or less' because if there are free variables in the body,
they must instantiate to a single value in the calling context
        ?- maplist( \(A):-A=B, [a,a,a,a]).
succeeds with B=a while
        ?- maplist( \(A):-A=B, [a,b,a,a]).
fails. However, the body can include an explicit existential
quantification so that, eg
        ?- maplist( \(A):- B^(A=B), [a,b,c,d]).
succeeds leaving B unbound.

Implementation is based on a replacement call/N predicate;
without the obvious bits and without the extra clauses to handle
existential quantification, it looks like this:

        maplist(P,[A|AX]) :- call(P,A), maplist(P,AX).
        maplist(P,[A|AX],[B|BX] :- call(P,A,B), maplist(P,AX,BX).
        % etc.

        call(P,A) :- mkcall(P, \(A), G), call(G).
        call(P,A,B) :- mkcall(P, \(A,B), G), call(G).
        call(P,A,B,C) :- mkcall(P, \(A,B,C), G), call(G).
        % etc.

        mkcall(Head:-Body,Args,Goal) :- copy_term(Head:-Body,Args:-Goal).


- Samer




------------
For further info, please visit http://www.swi-prolog.org/

To unsubscribe, send a plaintext mail with "unsubscribe prolog <e-mail>"
in its body to majordomo@...

 « Return to Thread: maplist and friends

LightInTheBox - Buy quality products at wholesale price