爱因斯坦之谜使用Prolog [英] Einstein Riddle using Prolog

查看:169
本文介绍了爱因斯坦之谜使用Prolog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Prolog解决爱因斯坦之谜.当我尝试按房屋运行时,它显示为否".任务为

I'm trying to solve the Einstein riddle using Prolog. When I'm trying to run by houses(Hs), it shows No. Task is

  1. 英国人住在红色的房子里.
  2. 瑞典人养狗作为宠物.
  3. 丹麦人喝茶.
  4. 绿房子在白房子的左侧.
  5. 温室的主人喝咖啡.
  6. 抽烟颇尔购物中心的主人饲养鸟类.
  7. 黄房子的老板抽着敦希尔(Dunhill)的烟.
  8. 住在中心房子里的主人喝牛奶.
  9. 挪威人住在第一所房子.
  10. 抽烟的人与养猫的人住在一起.
  11. 养马的主人与抽着Dunhill的烟的人住在一起.
  12. 抽烟Bluemasters的所有者喝啤酒.
  13. 德国人抽王子.
  14. 挪威人住在蓝色的房子旁边.
  15. 抽烟的人与喝水的人住在一起.
  1. The Brit lives in the red house.
  2. The Swede keeps dogs as pets.
  3. The Dane drinks tea.
  4. The green house is on the immediate left of the white house.
  5. The green house's owner drinks coffee.
  6. The owner who smokes Pall Mall rears birds.
  7. The owner of the yellow house smokes Dunhill.
  8. The owner living in the center house drinks milk.
  9. The Norwegian lives in the first house.
  10. The owner who smokes Blends lives next to the one who keeps cats.
  11. The owner who keeps the horse lives next to the one who smokes Dunhill.
  12. The owner who smokes Bluemasters drinks beer.
  13. The German smokes Prince.
  14. The Norwegian lives next to the blue house.
  15. The owner who smokes Blends lives next to the one who drinks water.


    houses(Hs) :-
       length(Hs, 5),                                            
       member(h(english,_,_,_,red), Hs),                         
       member(h(swede,dog,_,_,_), Hs),                         
       member(h(_,_,_,coffee,green), Hs),                        
       member(h(dane,_,_,tea,_), Hs),                       
       next(h(_,_,_,_,green), h(_,_,_,_,white), Hs),             
       member(h(_,bird,'Pall Mall',_,_), Hs),                       
       member(h(_,_,'Dunhill',_,yellow), Hs),                         
       Hs = [_,_,h(_,_,_,milk,_),_,_],                           
       Hs = [h(norwegian,_,_,_,_)|_],                            
       next(h(_,horse,_,_,_), h(_,_,'Dunhill',_,_), Hs),        
       next(h(_,_,blend,_,_), h(_,cat,_,_,_), Hs),             
       member(h(_,_,'Blue Master',beer,_), Hs),                        
       member(h(german,_,'Prince',_,_), Hs),                      
       next(h(norwegian,_,_,_,_), h(_,_,_,_,blue), Hs),  
       next(h(_,_,'Blend',_,_), h(_,_,_,water,_), Hs), 
       member(h(_,fish,_,_,_), Hs).

    next(A, B, Ls) :- append(_, [A,B|_], Ls).
    next(A, B, Ls) :- append(_, [B,A|_], Ls).

我不知道怎么了.谢谢

推荐答案

这是程序的概括.我添加了一些额外的*以删除多个目标,并用_/*origterm*/替换了一些术语.但是,生成的程序仍然失败.因此,错误必须在其余片段中.您没有说任何有关程序的信息(稍后添加了一些内容),所以我不(想要编辑)想知道它的含义.但是无论如何,该错误必须存在于其余可见部分:

Here is a generalization of your program. I added some extra * to remove several goals and replaced some terms by _/*origterm*/. And yet, the resulting program is still failing. Therefore, the error has to be in the remaining fragment. You did not say anything about the program ( you added something later), so I do not ( want to) know what it is about. But no matter what, the error has to be in the remaining visible part:

:- initialization(houses(_Sol)).
:- op(950, fy, *).
*_.

houses(Hs) :-
   length(Hs, 5),
   * member(h(english,_,_,_,red), Hs),                         %  2
   * member(h(swede,dog,_,_,_), Hs),
   * member(h(_,_,_,coffee,green), Hs),
   * member(h(dane,_,_,tea,_), Hs),
   * next(h(_,_,_,_,green), h(_,_,_,_,white), Hs),
   member(h(_,_/*bird*/,'Pall Mall',_,_), Hs),
   member(h(_,_,'Dunhill',_,_/*yellow*/), Hs),
   * Hs = [_,_,h(_,_,_,milk,_),_,_],
   * Hs = [h(norwegian,_,_,_,_)|_],
   * next(h(_,horse,_,_,_), h(_,_,'Dunhill',_,_), Hs),
   next(h(_,_,blend,_,_), _/*h(_,cat,_,_,_)*/, Hs),
   member(h(_,_,'Blue Master',_/*beer*/,_), Hs),
   member(h(_/*german*/,_,'Prince',_,_), Hs),
   * next(h(norwegian,_,_,_,_), h(_,_,_,_,blue), Hs),
   next(h(_,_,'Blend',_,_), _/*h(_,_,_,water,_)*/, Hs),
   * member(h(_,fish,_,_,_), Hs).

next(A, B, Ls) :- append(_, [A,B|_], Ls).
next(A, B, Ls) :- append(_, [B,A|_], Ls).

剩下的不多了!在可见部分至少有一个错误! (而且,严格来说,其他部分可能会有更多错误.我们只是不知道).

There is not much left! In the visible part there is at least one error! (And, strictly speaking there may be many more errors in the other parts. We simply don't know).

这篇关于爱因斯坦之谜使用Prolog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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