如何打印所有事实? [英] How to print all the facts?

查看:100
本文介绍了如何打印所有事实?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个问题而苦恼...

I am stuck for this problem...

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

当前,room3有钥匙和书. 我想打印钥匙和书. 我尝试了这段代码,显然只打印了一个. (只是键)

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是试图打印所有这些事实的代码. 我该如何处理? 任何帮助将是巨大的!谢谢.

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

推荐答案

查找并显示所有项目.

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).

实际上,您可以按照任何需要的方式实现show_items(Items).

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

这篇关于如何打印所有事实?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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