首页 > 解决方案 > 如何访问 PROLOG 中的规则数据

问题描述

我必须确定两个矩形是否重叠,我可以做到这一点,但我正在努力弄清楚如何获取给定的数据,并将其相互比较以确定更大的值。

%This is :what would be happening :
%separate(rectangle(0,10,10,0), rectangle(4,6,6,4))

separate(R1,R2) :-
    %I Have to figure out how to take the values from R1 and R2 and compare
    %them to one another.
.

标签: prolog

解决方案


它被称为“模式匹配”。

separated(R1, R2) :-
    R1 = rectangle(A1, B1, C1, D1),
    R2 = rectangle(A2, B2, C2, D2),
    /* now just use your As and Bs */

在许多情况下,最好直接写:

separated(rectangle(A1, B1, C1, D1), rectangle(A2, B2, C2, D2)) :-
    /* now just use your As and Bs */

推荐阅读