Re: Using foreach/2
"Abdallah, Samer" <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 21 Mar 2014, at 10:12, Boris Vassilev <[email protected]> wrote: > >> I wonder however whether >> there is a set of neat higher oder predicates that could be used for >> processing compound terms? >> > > This is what I was wondering exactly.... At some point some time ago I > noticed that arg/3 is really efficient with either the first or the third > argument (!) instantiated. So I am basically using a term as a "random > access" list, as this fits very nicely with the problem I was trying to > model. > > So do you mean predicates with semantics similar to those in > library(lists), but applied on compound terms? How come those are not > readily available (or to rephrase, why hasn't anyone needed those)? There > certainly is some sort of gotcha? Hello Boris, I have, on occasion, found use for a mapargs/{2,3,4,…} which works just like maplist/{2,3,4,…}. For example, mapargs/3 is %% mapargs(P:pred(A,B), T1:array(F,N,A), T2:array(F,N,B)) is nondet. mapargs(P,T1,T2) :- ( nonvar(T1) -> functor(T1,F,N), functor(T2,F,N) ; functor(T2,F,N), functor(T1,F,N) ), mapargs_x(1,N,P,T1,T2). mapargs_x(I,N,P,T1,T2) :- ( I>N -> true ; arg(I,T1,X1), arg(I,T2,X2), call(P,X1,X2), succ(I,J), mapargs_x(J,N,P,T1,T2) ). (I've made up type array(F,N,A), which is a term with functor F/N and all arguments of type A.) As you can see, there are some questions about what should be the policy on the head functors and how to handle input vs output terms. Maybe the caller should be responsible for sorting out the functors. Samer. > > Cheers, > Boris > -------------- next part -------------- > HTML attachment scrubbed and removed > _______________________________________________ > SWI-Prolog mailing list > [email protected] > https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog > -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 495 bytes Desc: Message signed with OpenPGP using GPGMail URL: <https://lists.iai.uni-bonn.de/pipermail/swi-prolog/attachments/20140321/05a0e340/attachment.bin>