我在prolog中的代码出现在相反的预期顺序。下面是代码:为什么我的Prolog代码反向打印?
room(homermargeroom, donuts).
room(homermargeroom, tv).
room(ensuite, nothing).
room(lisaroom, beer).
room(bartroom, donuts).
room(hallway, nothing).
room(bathroom, nothing).
room(maggieroom, nothing).
/* These are the locations where Bart, Lisa and Maggie are hiding */
hiding(bart, cupboard).
hiding(lisa, ensuite).
hiding(maggie, bathroom).
canHomerGet(Start, End, Item) :-
homermove(Start, End),
canTravelThrough(Start, Item),
canTravelThrough(End, Item),
write('Homer moves from '), write(Start), write(' to '), write(End), nl.
canHomerGet(Start, End, Item) :-
homermove(Start, Somewhere),
canTravelThrough(Somewhere, Item),
canHomerGet(Somewhere, End, Item),
write('Homer moves from '), write(Start), write(' to '), write(Somewhere), nl.
canTravelThrough(Somewhere, _Item) :-
room(Somewhere, nothing).
canTravelThrough(Somewhere, Item) :-
room(Somewhere, tv),
Item == portableTV.
canTravelThrough(Somewhere, Item) :-
room(Somewhere, donuts),
Item == nachos.
canTravelThrough(Somewhere, Item) :-
room(Somewhere, sistersinlaw),
Item == blindfold.
canTravelThrough(Somewhere, Item) :-
room(Somewhere, beer),
Item == margarita.
canHomerFind(Child, Item) :-
hiding(Child, Destination),
canHomerGet(garage, Destination, Item).
这里是输出:从门厅
荷马移动到橱柜
荷马从餐室移动到门厅
荷马从厨房移动到餐室
荷马从移动sidehall到厨房
荷马从车库移到侧厅
我写这个的方式,我希望能打印出'Homer从车库移动到sidehall'的拳头,然后按照与它相反的顺序打印该列表。对于如何解决这个问题,有任何的建议吗?
你确定你提供了所有的源代码吗?这个输出是由哪个查询产生的? –