本文介绍了解决Prolog中的逻辑难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读立即学习Prolog",以下是我自己无法解决的练习之一:
I am reading "Learn Prolog Now" and one of its exercises I haven't been able to solve myself is the following:
- 英国人住在红房子里.
- 美洲虎是西班牙家庭的宠物.
- 日本人住在蜗牛饲养者的右边.
- 蜗牛守护者住在蓝房子的左边.
- 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中的逻辑难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!