« Return to Thread: maplist and friends

maplist and friends

by Attila Bartha :: Rate this Message:

Reply to Author | View in Thread

i'm experimenting with variations on maplist and friends.
i would like more control on which arguments of the predicate will be unified with the elements of the lists.
i have came up with the following:

vmaplist(Pred, LA) :-
        term_variables(Pred, VL),
        vmaplist_a(Pred, VL, LA).

vmaplist_a(_, _, []) :- !.
vmaplist_a(Pred, VL, [A|As]) :-
        copy_term((Pred, VL), (Q, [A])),
        call(Q),
        vmaplist_a(Pred, VL, As).

vmaplist(Pred, LA, LB) :-
        term_variables(Pred, VL),
        vmaplist_a(Pred, VL, LA, LB).

vmaplist_a(_, _, [], []) :- !.
vmaplist_a(Pred, VL, [A|As], [B|Bs]) :-
        copy_term((Pred, VL), (Q, [A, B])),
        call(Q),
        vmaplist_a(Pred, VL, As, Bs).

in vmaplist/2,3 the (copy of the) free variable(s) which appear in Pred will be successively unified with the elements of the list(s) and (a copy of) Pred will be called.

in vmaplist_a Pred can have more free variables than the number of lists, and an additional list in the 2-nd position of vmaplist_a holds the vars which will be unified with the elems of the lists.

some examples:

:- vmaplist(X<10, [1,2,3]).
true.

:- vmaplist(A=B, [1,2,3], L).
L = [1,2,3].

:- vmaplist(A-B=C, [1,3,4,2], [foo,bar,baz,kix], L),
keysort(L, S),
vmaplist(A=B-C, S, X, Y).
L = [1-foo, 3-bar, 4-baz, 2-kix],
S = [1-foo, 2-kix, 3-bar, 4-baz],
X = [1, 2, 3, 4],
Y = [foo, kix, bar, baz].

vmaplist_a((numlist(1,N,L),format('~w ~w~n',[N, L])), [N], [1,3]).
1 [1]
3 [1, 2, 3]

vmaplist has about half the speed of maplist.
i suppose a lot of garbage is created because of copy_term.

vmaplist relies on the observation that term_variables gives the free vars in the order in which they appear in the text of Pred.
i have tested this on cases like :
  term_variables(pred(A, foo(B)), L). =>  L= [A,B]).
  term_variables(pred(foo(A), B)), L). => L = [A,B]).
it seems to be ok, but i guess is implementation specific, so it might be a problem.

i would like to hear your comments on this.
maybe other names for 'vmaplist', 'vmaplist_a' ?

-attoparsec-


      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

------------
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