prolog数据库从终端读取输入并从当前数据库中撤回它

时间:2013-01-11 05:38:08

标签: database io prolog

在下面的代码中,我想找到一种首先阅读角色的方法    (通过用户键入“3.”然后从中删除/撤消该字符的编号    listCharacters数据库

 :- dynamic listCharacters/1.  

 listCharacters(Joe).  
 listCharacters(Tom).  
 listCharacters(Peter).  

 :- write_ln('Type in the name of the character you have from the below list.    
  Example "Tom" '), write_ln('1. Joe'), write_ln('2. Tom'), write_ln('3. Peter'),   
  read(X), retract(listOfCharacters(X)).  

1 个答案:

答案 0 :(得分:0)

首先,您应该阅读列表中的元素,然后根据该内容构建菜单,例如

:- dynamic listCharacters/1.

listCharacters('Joe').
listCharacters('Tom').
listCharacters('Peter').

menu :-
    findall(Elem, listCharacters(Elem), L),

    format('Type in the index of the character you have from the below list.~n', []),
    forall(nth1(I,L,E), format('~d. ~q~n', [I, E])),

    read(X),
    (   nth1(X,L,E),
        retract(listCharacters(E))
    ->  true
    ;   format('cannot remove ~q', [X])
    ).

我已经介绍了一个menu程序,如果你坚持在文件底部请咨询添加:- menu.。一些测试:

?- menu.
Type in the index of the character you have from the below list.
1. 'Joe'
2. 'Tom'
3. 'Peter'
|: 2.
true.

?- menu.
Type in the index of the character you have from the below list.
1. 'Joe'
2. 'Peter'
|: 1.
true.