拆分列表并在序言中进行迭代 [英] Splitting list and iterating in prolog
问题描述
我正在尝试做似乎很简单的事情,但是我无法理解.我想从给定谓词中拆分序言中的列表,并遍历对象. 示例:
Im trying to do something which seems to be really simple but i cant get my head around. I want to split a list in prolog from given predicates and iterate over the objects. Example:
object_properties(jackass, [comedy, -australian]).
object_properties(the_godfather, [drama, crime, -character_batman]).
如何遍历列表并将其打印到屏幕上?更具体地说,我需要问用户该对象是否具有该属性.如果他们说是",则继续前进到列表中的下一个项目;如果他们说否",则继续前进到下一个对象.
How can i iterate over the lists and print it to the screen? More specificaly i need to ask the user if the object has the property. If they say yes move on to the next item in the list, if they say no move on to the next object.
任何帮助将不胜感激.
推荐答案
类似的东西可以帮助您开始
something like this could help you to start
object_properties :-
object_properties(O, Ps),
query_user_loop(O, Ps).
query_user_loop(_, []).
query_user_loop(O, [P|Ps]) :-
write([object, O, has, P, ?]),
read(Answer),
( Answer == yes
-> query_user_loop(O, Ps)
).
object_properties(jackass, [comedy, -australian]).
object_properties(the_godfather, [drama, crime, -character_batman]).
这样做是为了进行简单的交互(请注意每个答案后的点):
this does for simple interaction (please note the dot after each answer):
9 ?- object_properties.
[object,jackass,has,comedy,?]yes.
[object,jackass,has,-australian,?]no.
[object,the_godfather,has,drama,?]yes.
[object,the_godfather,has,crime,?]yes.
[object,the_godfather,has,-character_batman,?]yes.
true
这篇关于拆分列表并在序言中进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!