Prolog,标识项目的列表位于当前位置

时间:2012-11-27 19:08:45

标签: list prolog win-prolog

我无法解决如何在prolog中写出胸部“C”位于“L”的位置 这是我目前的代码,但我认为我太复杂了,走向了错误的方向

    location(C, L).

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

任何人都可以帮助或指出我正确的方向来解决这个问题。

要检查它,我使用此代码:

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

我现在已经改变了它并且:

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

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

但它似乎没有用,我错过了什么?

2 个答案:

答案 0 :(得分:1)

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

然后你会得到

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

答案 1 :(得分:1)

这是另一个,虽然CapelliC解决方案很好。

location(C, [(C,P)|_], P):-!.
location(C, [_|T], P) :- location(C, T, P).
相关问题