

I believe the predicate 'include' is very hard to find. The common terminology in the functional context of 'map' and 'fold' is 'filter'.
Did you know ... | Search Documentation: |
![]() | Predicate include/3 |
call(Goal, Xi)
succeeds.
is_odd(I) :- 0 =\= I mod 2.
?- numlist(1, 6, List), include(is_odd, List, Odd). List = [1, 2, 3, 4, 5, 6], Odd = [1, 3, 5].
Example:
We want the Keys of Key-Value pairs which match a certain Value.
Data:
all_pairs([winter-1, summer-2, autumn-3, spring-4, hot-2, cold-1, flowers-4, mud-3]).
Code:
% Create the filter predicate for include/3. % It will be used in the form of a "closure" (in the Prolog sense), i.e. % it will be given to include/3 as a goal with the leftmost arguments already % prefilled with "Value". my_filter(Value,_-Value). % Succeed for "Key-Value" if it ends in "Value" % Given a "Value", and a list of "Pairs", this predicate relates the "Value" % to a list "KeysOfInterest" whereby an element can be found in "KeysOfInterest" % iff it is the "Key" of a "Pair" from "Pairs" which has value "Value". keys_with_value(Value,Pairs,KeysOfInterest) :- Closure=my_filter(Value), include(Closure,Pairs,PairsOfInterest), % Filter Pairs, retaining only those which pass filter/2 maplist([K-_,K]>>true,PairsOfInterest,KeysOfInterest). % Retain only the keys of the "PairsOfInterest"
Run it:
?- all_pairs(Pairs),keys_with_value(2,Pairs,Keys). Pairs = [winter-1, summer-2, autumn-3, spring-4, hot-2, cold-1, flowers-4, mud-3], Keys = [summer, hot].
Alternatively one can define filter/2 inline using a library(yall) Lambda expression, just as we did for maplist/2:
keys_with_value_2(Value,Pairs,KeysOfInterest) :- include({Value}/[K-V]>>(V==Value),Pairs,PairsOfInterest), maplist([K-_,K]>>true,PairsOfInterest,KeysOfInterest).