我有一个关于 Prolog 的测试,但我无法真正掌握它的基本思想。我有点理解我经历过的一些例子,但我不能坐下来立即知道如何解决一个特定的问题。
我们的教授给了我们一些例子,我想知道是否有人可以指导我如何做到这一点,所以我有一些想法如何处理这样的事情。
这个例子来自我们的书:
Donna, Danny, David, and Doreen were seated at a table.
The men sat across from each other, as did the women.
They each ordered a different drink and main course.
Facts:
Doreen sat beside the person that ordered steak
The chicken came with a coke
The person with lasagna sat across from the person with milk
David never drinks coffee
Donna only drinks water
Danny could not afford to order steak
我尝试了一些方法,每个人都有一个与他们相关的列表,你会填写事实,但我认为这不是正确的方法。有人可以帮我完成这个吗?谢谢!
编辑:
这是我最终得到的代码,它完成了大部分难题,它留下了一份主菜和一份饮料,但这应该是可以修复的:
sat_across([X,_,Y,_], X, Y).
sat_across([_,X,_,Y], X, Y).
sat_beside(T, X, Y) :- % this is tricky
nth1(N,T,X), nth1(M,T,Y),
(N =:= M+1 ; N =:= M-1 ; N == 1, M == 4 ; N == 4, M == 1).
not_connected(T, Place) :- \+ member(Place, T).
connected(T, Place) :- member(Place, T).
solve(T) :- T = [_,_,_,_],
sat_across(T, (danny,_,_), (david,_,_)),
sat_across(T, (donna,_,_), (doreen,_,_)),
sat_beside(T, (doreen,_,_), (_,_,steak)),
connected(T, (_,coke,chicken)),
sat_across(T, (_,_,lasagna), (_,milk,_)),
not_connected(T, (david,coffee,_)),
connected(T, (donna,water,_)),
not_connected(T, (danny,_,steak)).