序言中的知识统一

knowledgment unification in prolog

我要做的就是统一可能的选项,用这几句话解决问题

  1. The Spaniard lives next to the red house.
  2. The Norwegian lives in the blue house.
  3. An Italian lives in the second house.

这是我的尝试,但出现错误,有人可以帮助我吗?

neighborhood(N):-
  length(V,3),
  next(house(_,spaniard), house(red,_), V),
  member(house(blue,norway), V),
  V = [_|house(_,italian)].

您可以编写一个程序来执行您的每条规则,然后让 prolog 找到满足所有这些规则的房屋的可能排序:

neiborhood(Houses):-
 Houses=[House1, Italy, House3], % these are the houses, after rule 3
 Italy=house(_ItalyColor, italy),
 Spain=house(_SpainColor, spain),
 
 % rule 2:
 Norway=house(blue, norway),
 member(House1-House3, [Spain-Norway, Norway-Spain]),

 % rule 1:
 append(_, [HouseA, HouseB|_], Houses),
 (HouseA-HouseB=Spain-house(red, _) ; HouseB-HouseA=Spain-house(red, _)).

在这段代码中,当您说西班牙人住在红房子旁边时,我假设它可能住在那所房子的“左边”或“右边”。 另请注意,您只提到了 2 种房屋颜色,因此第三种颜色未分配。也许您还遗漏了另一条规则,可能是缺少颜色。

样本运行:

?- neiborhood(Houses).
Houses = [house(_163550, spain), house(red, italy), house(blue, norway)] ;
Houses = [house(blue, norway), house(red, italy), house(_163550, spain)] ;
false.

在这两种解决方案中,西班牙房子都没有分配任何颜色。