Semantic and Declarative Technologies Course, Homework P3, some hints


This page gives some hints on the predicate pairings/2 of Homework P3.

Write the following helper predicate:
 
% pair_list(E, L, PL): PL is a list of pairs E-X, for each element E of L.
 
| ?- pair_list(a, [b,c,d], Ps).
Ps = [a-b,a-c,a-d] ? ;
no
| ?- pair_list(a, [], Ps).
Ps = [] ? ;
no
 
Next, write another helper predicate that takes a list L as input, say [a,b,c,d], and calls pair_list with each element of L and the list of subsequent elements. The output of this second helper will be a list of lists returned by the invocations of pair_list, i.e. [[a-b,a-c,a-d],[b-c,b-d],[c-d],[]].
 
You can then implement pairings/2 by calling the second helper and then flattening its output using append/2. (Note that there is a flatten/2 predicate in SWI which is not available in SICStus, therefore you should use append/2, which is available in both systems, and is a bit more efficient than flatten/2). Don't forget to load library lists by including this line at the beginning of the code: :- use_module(library(lists)).

Last modified by Péter Szeredi, szeredi@cs.bme.hu, on 03-15-2022.