Splitting list and iterating in prolog

只愿长相守 提交于 2019-12-10 18:16:15

问题


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.

Any help would be greatly appreciated.


回答1:


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 


来源:https://stackoverflow.com/questions/19886637/splitting-list-and-iterating-in-prolog

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