next up previous index
Next: CIO Up: Libraries Previous: quintus_util

Apply Macros

     

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

:- lib(apply_macros).
It provides the following macros:
maplist(Pred, ListIn, ListOut)
  calls Pred with two additional arguments on every element of the ListIn, yielding ListOut. For example
[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.

checklist(Pred, List)
  calls Pred with one additional argument on every element of List.

[eclipse 1]: checklist(<(0), [1, 2, 3]).
*** Creating auxiliary predicate 'checklist(<(0))' / 1

yes.

selectlist(Pred, ListIn, ListOut)
  stores in ListOut all element from ListIn which satisfy Pred

[eclipse 1]: selectlist(<(5), [1, 6, 8, 3, 9, 5], L).
*** Creating auxiliary predicate 'selectlist(<(5))' / 2

L = [6, 8, 9]
yes.

sumlist(Pred, List, AccIn, AccOut)
  calls Pred enhanced by three arguments on every element of List, using an accumulator, i.e. if List = , the following sequence is called:
Pred(X1, AccIn, A1),
Pred(X2, A1, A2),
..
Pred(Xn, An-1, AccOut).
e.g.
[eclipse 1]: sumlist(times, [2, 3, 4, 5], 1, X).
*** Creating auxiliary predicate 'sumlist(times)' / 3

X = 120
yes.

mapargs(Pred, TermIn, TermOut)
  is similar to maplist, but traverses the arguments of TermIn rather than a list, e.g.
[eclipse 1]: mapargs(atom_string, s(a,b,c), X).
*** Creating auxiliary predicate 'mapargs(atom_string)' / 2

X = s("a", "b", "c")
yes.

sumargs(Pred, Term, AccIn, AccOut)
  is similar to sumlist, but traverses the arguments of Term rather than a list, e.g.
[eclipse 1]: sumargs(times, s(1,2,3,4), 1, X).
*** Creating auxiliary predicate 'sumargs(times)' / 3

X = 24
yes.

sumnodes(Pred, Term, AccIn, AccOut)
  is like sumlist/4, but Term may be any term and Pred is called recursively on all its subterms. Example: collect all variables in a term
[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.

mapstream(Pred, ListIn, ListOut)
  is similar to maplist, but delays if the input list is a variable, e.g. in coroutine mode:
[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.



next up previous index
Next: CIO Up: Libraries Previous: quintus_util



Micha Meier
Mon Mar 4 12:11:45 MET 1996