I have been working on this code to look for the location and tell me if the location exists. The code is:
location(C, L).
location(C, [C,L]|_]).
location(C, [_|T]):- location(C,T,L).
and i want it to be true if C appears as a chest in the location L. I type in the following code which is:
location(b,[(a, 1), (b,2)]).
The answer should be given as a yes as its found that the chest is in the location. This does give me a yes but when i change the code to:
location(e, [(a,1), (b,2)]).
I still get a yes when it should state a no. Does anyone know what im missing?
Hello I’ll try to answer your question and point out at your errors.
Your first line says any C is element of L. Unification in Prolog is done from the top towards the bottom, and from left to right. So Prolog will always unify that line, hence it will return true always. Your second and third line contain syntax errors. Anyways here is the code that should answer your question :
% here we say that no chest can be contained in an empty location
% if we somehow manage to get to a head that contains C thats true
% here we say if C is not equal to Something(from the Head) try to look for C in the Tail
You can also solve it in the following manner
%basically you ask is there a member Chest in L such that Chest=(C,_).