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

Database prolog数据库从终端读取输入并从当前数据库中收回,database,io,prolog,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. E

在下面的代码中,我想找到一种先读入字符的方法
(通过用户键入“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)).  

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

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