List comprehensions: Difference between revisions

Line 729:
SWI-Prolog does not have list comprehension, however we can simulate it.
 
<lang Prolog>:-% op(700,We xfx,need <-).operators
:- op(700, xfx, <-).
:- op(450, xfx, ..).
:- op(1100, yfx, &).
 
% we need to define the intervals of numbers
Vs <- M..N :-
integer(M),
Line 739 ⟶ 741:
between(M, N, Vs).
 
% finally we define list comprehension
% prototype is Vs <- {Var, Dec, Pred} where
% Var is the list of variables to output
% Dec is the list of intervals of the variables
% Pred is the list of predicates
Vs <- {Var & Dec & Pred} :-
findall(Var, maplist(call, [Dec, Pred]), Vs).
Anonymous user