To calculate the hamming distance between two lists of the same length, I use foldl(hamm, A, B, 0, R).
with this definition of hamm/4
:
hamm(A, A, V, V) :- !.
hamm(A, B, V0, V1) :- A \= B, V1 is V0 + 1.
The cut in the first rule prevents the unnecessary backtracking. The second rule, however, could have been written differently:
hamm2(A, A, V, V) :- !.
hamm2(_, _, V0, V1) :- V1 is V0 + 1.
and hamm2/4
will still be correct together with foldl/5
or for queries where both A and B are ground.
So is there a really good reason to prefer the one over the other? Or is there a reason to keep the rules in that order or switch them around?
I know that the query
hamm(a, B, 0, 1).
is false, while
hamm2(a, B, 0, 1).
is true, but I can't quite decide which one makes more sense . . .
hamm2(a, B, 0, 1)
, yes, B is not the same as A, so these two elements should add to the hamming distance... but as I said, I also can't quite decide when this would make sense. – Gargantuan