Map List and Term Operations¶
This library provides a set of utilities for applying a predicate to all elements of a list.
Auxiliary routines.
They allow one to easily perform the most common do-loop constructs in Prolog. To avoid performance degradation, each call creates an equivalent Prolog program, without meta-calls, which is executed by the Prolog engine instead. The library was based on code by Joachim Schimpf and on code from SWI-Prolog, and it is also inspired by the GHC libraries.
The routines are available once included with the use_module(library(maplist))
command. Examples:
plus(X,Y,Z) :- Z is X + Y.
plus_if_pos(X,Y,Z) :- Y > 0, Z is X + Y.
vars(X, Y, [X|Y]) :- var(X), !.
vars(_, Y, Y).
trans(TermIn, TermOut) :-
nonvar(TermIn),
TermIn =.. [p|Args],
TermOut =..[q|Args], !.
trans(X,X).
~~~~
%success
?- maplist(plus(1), [1,2,3,4], [2,3,4,5]).
?- checklist(var, [X,Y,Z]).
?- selectlist(<(0), [-1,0,1], [1]).
?- convlist(plus_if_pos(1), [-1,0,1], [2]).
?- sumlist(plus, [1,2,3,4], 1, 11).
?- maplist(mapargs(number_atom),[c(1),s(1,2,3)],[c( '1' ),s( '1' , '2' , '3' )]).