0

Im having trouble work out how to write in prolog that the chest "C" is at location "L" This is the code i have at the moment but i think im making it too complicated and going in the wrong direction

    location(C, L).

    location(C, [[C,L]|_]).
    location(C, [_|T]) :-
            location(C, T, L).

Can anyone help or point me in the right direction into solving this.

To check it, i use this code:

    location(b, [(a,10), (b,6), (c,8), (d,14)]).

I have now changed it and have:

    location(C, L, P). 
    location(C, L, P) :- memberchk((C,P), L).

and

    location(b, [(a,10), (b,6), (c,8), (d,14)], P).

But it doesnt seem to work, what have i missed out?

user1854914
  • 137
  • 8

2 Answers2

1

try

location(S, L, P) :- memberchk((S,P), L).

then you'll get

?- location(b, [(a,10), (b,6), (c,8), (d,14)], P).
P = 6.
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • I have now changed it and have: location(C, L, P). location(C, L, P) :- memberchk((C,P), L). and location(b, [(a,10), (b,6), (c,8), (d,14)], P). But it doesnt seem to work, what have i missed out? – user1854914 Nov 27 '12 at 21:52
  • sorry, I saw only late the win-prolog tag (I never heard of this system). Try member instead of memberchk. If that doesn't work, the answer by @iccthedral should be fine. – CapelliC Nov 27 '12 at 21:56
1

Here's another one, although CapelliC solution is just fine.

location(C, [(C,P)|_], P):-!.
location(C, [_|T], P) :- location(C, T, P).
nullpotent
  • 9,162
  • 1
  • 31
  • 42
  • @CapelliC Uh yes, that wasn't supposed to be there. – nullpotent Nov 27 '12 at 20:59
  • Its surpose to define a predicate location(C, L) which is true if C appears exactly as a chest in the location L. Example, location(a, [(a, 10), (b, 6), (c, 8), (d, 14)]). and the result will be: yes. – user1854914 Nov 28 '12 at 14:34
  • Well this does exactly that, and a bit more; it unifies your third argument with the 2nd argument of the tuple when a match is found. I suggest you study Prolog a bit more, it is obvious that you're confused ATM. – nullpotent Nov 28 '12 at 18:28
  • Yeah ive only just started to learn it and trying some examples to build my knowledge on it. – user1854914 Nov 28 '12 at 18:52