Prolog Bonus

Bonus material that does not fit elsewhere.

When is a goal true?

In Prolog, not matching one of the arguments of a function / goal / fact is not an error; the default is false. The function check in the program below is false, not an error.

abc(0) :- true.
check :- abc(5).

This explains why maplist is false when the arguments do not have the same length. The recursion rules for maplist remove one item from each list. There is no pattern in maplist for one empty and one nonempty list, so the false result comes directly from maplist.

d1 :-
    maplist(#=,[9],[9]).
d2 :-
    maplist(#=,[8],[]).

Pairs: Key-Value

The minus sign creates a key-value pair (an element in a dictionary in Python). Here is an example showing how to create a list of ordered pairs.

d3(Xs,Xnums) :-
    length(Xs,N),
    numlist(1,N,Nums),
    maplist([Xn,X,N]>>(Xn = X-N), Xnums, Xs, Nums).
%% ?- d3([apple, blueberry, goat], Numbered).
% Numbered = [apple-1, blueberry-2, goat-3].
Last modified February 22, 2024: Prolog reference and fact sheet. (08586b3)