在 Prolog 中解决逻辑难题 [英] Solving logic puzzle in Prolog

查看:16
本文介绍了在 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:

  • 英国人住在红房子里.
  • 美洲虎是西班牙家庭的宠物.
  • 日本人住在蜗牛饲养员的右边.
  • 蜗牛饲养员住在蓝房子的左边.

斑马是谁养的?

定义一个谓词zebra/1,告诉您斑马主人的国籍.

Define a predicate zebra/1 that tells you the nationality of the owner of the zebra.

提示:想想房屋和街道的表示.在 Prolog 中对四个约束进行编码.membersublist 可能是有用的谓词.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆