我在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'的拳頭,然後按照與它相反的順序打印該列表。對於如何解決這個問題,有任何的建議嗎?
你確定你提供了所有的源代碼嗎?這個輸出是由哪個查詢產生的? –