简体   繁体   English

拆分列表并在prolog中迭代

[英]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. 我想从给定的谓词中拆分prolog中的列表并迭代对象。 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. 如果他们说是,则转到列表中的下一个项目,如果他们说没有移动到下一个对象。

Any help would be greatly appreciated. 任何帮助将不胜感激。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM