解决Prolog中的逻辑难题 [英] Solving logic puzzle in Prolog
问题描述
我正在阅读立即学习Prolog",以下是我自己无法解决的练习之一:
I am reading "Learn Prolog Now" and one of its exercises I haven't been able to solve myself is the following:
有一条三人行道 相邻的房子都有一个 不同的颜色.它们是红色,蓝色, 和绿色.不同的人 民族生活在不同的地方 房子,他们都有不同的地方 宠物.以下是一些有关的事实 他们:
There is a street with three neighboring houses that all have a different color. They are red, blue, and green. People of different nationalities live in the different houses and they all have a different pet. Here are some more facts about them:
- 英国人住在红房子里.
- 美洲虎是西班牙家庭的宠物.
- 日本人住在蜗牛饲养者的右边.
- 蜗牛守护者住在蓝房子的左边.
- The Englishman lives in the red house.
- The jaguar is the pet of the Spanish family.
- The Japanese lives to the right of the snail keeper.
- The snail keeper lives to the left of the blue house.
谁养着斑马?
定义谓词zebra/1
,该谓词告诉您斑马斑马所有者的国籍.
Define a predicate zebra/1
that tells you the nationality of the owner of the zebra.
提示:考虑一下房屋和街道的代表.在Prolog中编写四个约束. member
和sublist
可能是有用的谓词.
Hint: Think of a representation for the houses and the street. Code the four constraints in Prolog. member
and sublist
might be useful predicates.
有什么想法可以在Prolog中进行编码吗?谢谢.
Any ideas how to code it under Prolog? Thanks.
推荐答案
neigh(Left, Right, List) :-
List = [Left | [Right | _]];
List = [_ | [Left | [Right]]].
zebraowner(Houses, ZebraOwner):-
member([englishman, _, red], Houses),
member([spanish, jaguar, _], Houses),
neigh([_, snail, _], [japanese, _, _], Houses),
neigh([_, snail, _], [_, _, blue], Houses),
member([ZebraOwner, zebra, _], Houses),
member([_, _, green], Houses).
zebra(X) :- zebraowner([_, _, _], X).
这篇关于解决Prolog中的逻辑难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!