Very often the programmer wants to scan a given list or structure and apply a given predicate on every subterm, or even to collect some data from every subterm. Although it is quite easy to define the apply/2 or maplist/3 etc. predicates, their performance is poor because of the involved metacalls. ECLiPSe offers the possibility to use macros to define these operations, so that they have the efficiency of specialized predicates, i.e. as if the predicates were partially evaluated with respect to the given inputs. The library is loaded using
It provides the following macros::- lib(apply_macros).
[eclipse 1]: lib(apply_macros). loading the library /usr/local/eclipse/lib/apply_macros.pl yes. [eclipse 2]: maplist(times(3), [1, 2, 3], L). *** Creating auxiliary predicate 'maplist(times(3))' / 2 L = [3, 6, 9] yes.
[eclipse 1]: checklist(<(0), [1, 2, 3]). *** Creating auxiliary predicate 'checklist(<(0))' / 1 yes.
[eclipse 1]: selectlist(<(5), [1, 6, 8, 3, 9, 5], L). *** Creating auxiliary predicate 'selectlist(<(5))' / 2 L = [6, 8, 9] yes.
Pred(X1, AccIn, A1),e.g.
Pred(X2, A1, A2),
..
Pred(Xn, An-1, AccOut).
[eclipse 1]: sumlist(times, [2, 3, 4, 5], 1, X). *** Creating auxiliary predicate 'sumlist(times)' / 3 X = 120 yes.
[eclipse 1]: mapargs(atom_string, s(a,b,c), X). *** Creating auxiliary predicate 'mapargs(atom_string)' / 2 X = s("a", "b", "c") yes.
[eclipse 1]: sumargs(times, s(1,2,3,4), 1, X). *** Creating auxiliary predicate 'sumargs(times)' / 3 X = 24 yes.
[eclipse 1]: [user]. vars(X) --> {var(X)} -> [X]; []. user compiled traceable 260 bytes in 0.00 seconds yes. [eclipse 2]: sumnodes(vars, s(1,t(X,2),[Y]), List, []). *** Creating auxiliary predicate 'sumnodes(vars)' / 3 X = X Y = Y List = [X, Y] yes.
[eclipse 1]: mapstream(+(3), In, Out), In = [1,2,3|T]. *** Creating auxiliary predicate 'mapstream(+(3))' / 2 Out = [4, 5, 6|News0] In = [1, 2, 3|T] Delayed goals: 'mapstream(+(3))'(T, News0) yes.