Consider this:
list_length([], 0).
list_length([H|T], N) :- list_length(T, N - 1).
It doesn't give a compilation error, but it doesn't work.
my thinking is:
suppose i call list_length([1], N)
we query list_length([], N - 1)
it tells you that N - 1 = 0
it finds out N is 1 and returns that value of N.
Instead, with gprolog, it tells me no.
In particular, it is also surprising it compiles, given something like:
?- 1 - 1.
uncaught exception: error(existence_error(procedure,(-)/2),top_level/0)
-is not always an arithmetic operator.N - 1 = 0will not succeed withN = 1because=is not arithmetic constraint solver.If you are using gprolog,
N - 1 #= 0will give youN = 1. Your code will work with this:list_length([], 0)is equivalent tolist_length([], N) :- N = 0, so it all comes down to,=vs#=.=does unification,#=does arithmetic constraint solving.You can see in a already linked question, there are multiple better ways to do this.