GNU Prolog assert error

Use assertz/1 or asserta/1 instead. GNU-Prolog does not provide assert/1 because only asserta/1 and assertz/1 are defined in the standard. Note that while asserta/1 always had one clear interpretation meaning add the clause at the beginning, the meaning of assertz/1 was more difficult to resolve since “add a clause at the end” does not completely … Read more

PROLOG: Determining if elements in list are equal if order does not matter

As a starting point, let’s take the second implementation of equal_elements/2 by @CapelliC: equal_elements([], []). equal_elements([X|Xs], Ys) :- select(X, Ys, Zs), equal_elements(Xs, Zs). Above implementation leaves useless choicepoints for queries like this one: ?- equal_elements([1,2,3],[3,2,1]). true ; % succeeds, but leaves choicepoint false. What could we do? We could fix the efficiency issue by using … Read more

Prolog append with cut operator

It sometimes really makes sense to introduce green cuts — even into append/3, but care must be taken that such a cut remains a green cut. That is, a cut that does improve efficiency (on a certain level) and does not affect answers. There is a very simple rule-of-thumb for introducing green cuts: If you … Read more

Should I avoid tail recursion in Prolog and in general?

Short answer: Tail recursion is desirable, but don’t over-emphasize it. Your original program is as tail recursive as you can get in Prolog. But there are more important issues: Correctness and termination. In fact, many implementations are more than willing to sacrifice tail-recursiveness for other properties they consider more important. For example steadfastness. But your … Read more

‘if’ in prolog?

Yes, there is such a control construct in ISO Prolog, called ->. You use it like this: ( condition -> then_clause ; else_clause ) Here is an example that uses a chain of else-if-clauses: ( X < 0 -> writeln(‘X is negative. That’s weird! Failing now.’), fail ; X =:= 0 -> writeln(‘X is zero.’) … Read more