How to print all the facts?

北城余情 提交于 2019-12-24 13:41:30

问题


I am stuck for this problem...

isAt(keys, room3).
isAt(book, room3).
isAt(keys, room6).
isAt(keys, room4).

currently, room3 have keys and book. I want to print keys and book. I tried this code and apparently prints only one. (just keys)

look :- isIn(Location),
  write('You are in '),
  write(Location),
  nl,
  items_inroom(Location),
  nl.


items_inroom(Location) :-
    isIn(Location),
    isAt(Item, Location),
    write('Available Item(s):'), 
    write(Item),
    nl.

items_inroom(_) :-
    write('Available Item(s): None'),
    nl. 

items_inroom is the code that trying to print all these facts. How can I approach this? any help will be great! Thank you.


回答1:


Find all items and display them.

items_inroom(Location) :-
    write('Available Item(s):'),
    findall(Item, isAt(Item, Location), Items),
    show_items(Items).

show_items([]) :-
    write('None'), !.

show_items(Items) :- 
    write(Items).

Actually you can implement the show_items(Items) in any way you want.




回答2:


From Chapter 11 in "The Craft of Prolog" by Richard O'Keefe, a bit simplified/refactored to save keystrokes:

print_item_report(Location) :-
    (   setof(Item, isAt(Item, Location), Items)
    ->  format("Items available in ~w:~n", [Location]),
        forall(member(I, Items),
               format("~w~n", [I]))
        % print_item_report_footer
    ;   format("No items in ~w~n", [Location])
    ).

% etc

If you don't have format for whatever reason, you can still use write. If you don't have forall, then this:

forall(Condition, Action)

is defined as

\+ (Condition, \+ Action )

so you can use that instead. See the SWI-Prolog forall/2 documentation for details.




回答3:


items_inroom/1 predicate will always print the first occurrence of Item on all facts isAt/2. You need to loop over all the facts isAt/2, use a metapredicate setof/3, bagog/3 or findall/3, I will recomend setof/3 like @Boris did, or build your own bucle (maybe not the best idea, but it's an option):

show_items(Location):- isAt(Item, Location),   % Condition
                     write(Item), nl,          % Process result
                     fail.                     % force backtracking to evaluate condition and find a new result
show_items(_).                                 % return true when all options have been evaluated


来源:https://stackoverflow.com/questions/30949621/how-to-print-all-the-facts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!