Reputation:
I'm trying to write some rules that will tell about the number of blocks in a stack (not the data structure, just a location). The rule stack return true for every block in a stack. Specifically, is zero/one/two the number of blocks in X?
num_blocks(X,zero) :- \+stack(X,_).
num_blocks(X,one) :- stack(X,A),\+stack(X,B),A\=B.
So, num_blocks(X,zero)
works as intended, but the other one doesn't. My thought process is that if we can find one block(A) and we cannot find a second unique block(B), then there is only one block in X stack. However, I still keep getting false
back.
Upvotes: 0
Views: 41
Reputation: 22585
What you intended (as per your description) is to check whether there is no other block in the stack. But you are just applying not to stack(X,B) which means there is no block in the stack X, and it \+stack(X,B)
succeded it would leave B as is (uninstantiated).
I think what you want for the second clause is the negation of the conjunction of stack(X,B) and A\=B, that is:
num_blocks(X,one) :-
stack(X,A),
\+ ( stack(X,B), A\=B ).
Upvotes: 0