Did you know ... | Search Documentation: |
Pack prolog_library_collection -- prolog/pair_ext.pl |
Extends the support for pairs in the SWI-Prolog standard library.
The following predicates are exported from this file while their implementation is defined in imported modules or non-module files loaded by this module.
Deterministic if any argument is instantiated to a finite list and the others are either free or finite lists. All three lists are in the same order.
pairs_keys_values(Pairs, _, Values)
pairs_keys_values(Pairs, Keys, _)
?- group_pairs_by_key([a-2, a-1, b-4, a-3], X). X = [a-[2,1], b-[4], a-[3]]
Sorting the list of pairs before grouping can be used to group all values associated with a key. For example, finding all values associated with the largest key:
?- sort(1, @>=, [a-1, b-2, c-3, a-4, a-5, c-6], Ps), group_pairs_by_key(Ps, [K-Vs|_]). K = c, Vs = [3, 6].
In this example, sorting by key only (first argument of sort/4 is 1) ensures that the order of the values in the original list of pairs is maintained.
map_list_to_pairs(length, ListOfLists, Pairs),