Thanks to some great help in another thread, I have a basic understanding of choicepoints, but obviously still haven’t got it clear.
I was trying an exercise in a book, in which the aim was to write a predicate translate(Ints, Words)
that succeed if Ints
is a list of integers, and Words
is a translation of those integers into the word equivalents, so translate([1,2], [one, two])
would succeed.
I did my first attempt before asking that other question, and ended up with the following code…
means(0, zero).
means(1, one).
means(2, two).
% other clauses for means() omitted for clarity
translate([N], [W]) :-
means(N, W).
translate([H1,H2|T], Res) :-
means(H1, W),
translate([H2|T], Tt),
append([W], Tt, Res).
This works, but gives a choicepoint.
Having watched the video that @brebs linked in that other question (several times), I can’t understand why this gives a choicepoint, as the first argument to the two clauses seem to be clearly distinct. The way I currently understand it, the first argument of the first clause would only match a list of exactly one element, whereas the first argument of the second clause would only match a list of at least two elements.
Can anyone explain why this code gives a choicepoint?
Since watching that video a few times, I was able to write a different version that didn’t give a choicepoint…
tr([], []).
tr([H|T], Res) :-
means(H, W),
tr(T, Tt),
append([W], Tt, Res).
However, I’d still like to understand why my first version gives a choicepoint. Thanks for any help you can give.