Вопрос:

Trying to check lines are between 2 points that exist, Prolog

prolog line point

84 просмотра

1 ответ

66 Репутация автора

I am trying to check whether lines are actually between 2 points that exist in the rule set. For example in the code below,

point(a).
point(z).

line(l,k).
line(k,l).
line(k,a).
line(a,z).

line(a,z). is a real line because a and z are both points in the rule set but line(l,k). isn't a real line because l and k aren't real points in the rule set.

I am new to Prolog and would just like some pointers for how to do this.

Автор: Ollu_ Источник Размещён: 08.11.2017 10:16

Ответы (1)


5 плюса

10743 Репутация автора

Решение

You could write:

real_line(X,Y):-line(X,Y),point(X),point(Y).

The , in Prolog means logical "and" so the above predicate real_line/2 succeeds when for X,Y holds: line(X,Y) and X is a point and Y is a point.

Some examples:

?- real_line(a,z).
true.

?- real_line(a,k).
false.

?- real_line(l,k).
false.

?- real_line(X,Y).
X = a,
Y = z.

?- real_line(X,z).
X = a.

?- real_line(a,Y).
Y = z.

UPDATE

Based on your comments to find all incorrect lines you could use:

findall(line(X,Y),(line(X,Y), \+ real_line(X,Y)),L).

Example:

?- findall(line(X,Y),(line(X,Y), \+ real_line(X,Y)),L).
L = [line(l, k), line(k, l), line(k, a)].

OR with a fail-driven loop:

write_incorrect_lines:-
            line(X,Y),
            \+real_line(X,Y),
            write(line(X,Y)),
            nl,
            fail;
            true.

Example:

?- write_incorrect_lines.
line(l,k)
line(k,l)
line(k,a)
true.
Автор: coder Размещён: 08.11.2017 10:28
Вопросы из категории :
32x32