如何将用户输入添加到 Prolog 中的列表,然后能够稍后查看列表

How to add user input to a list in Prolog then be able to view the list later

“这是我的代码,我不知道如何将数据放入 'student_list' 以便将其保存在那里,然后我可以稍后搜索它。” 有人知道怎么做吗?

main:-  
    nl, put_char('*'), put_char('M'), put_char('A'), put_char('I'), put_char('N'), put_char(' '),
    put_char('M'),put_char('E'),put_char('N'),put_char('U'), put_char('*'), nl, nl,
    
    write('Enter number of option you wish to do.'),nl,
    write('[1] Add New Student'),nl,
    write('[2] Search Student Record'),nl,
    write('[3] EXIT'),nl,
    write('Option : '),
    read(X), nl, answer(X).
    answer(1):- add.
    answer(2):- search.
    answer(3):- put_char('B'), put_char('Y'), put_char('E'), put_char('!'), nl, write('Program Terminated.'), !.
    
add:-
    nl, put_char('*'), put_char('A'), put_char('D'), put_char('D'), put_char(' '), put_char('S'), put_char('T'),
    put_char('U'),put_char('D'),put_char('E'), put_char('N'),put_char('T'), put_char('*'), nl, nl,
    
    write('Enter ID Number :'),tab(1),read(Idnumber),
    write('Enter First Name :'),tab(1),read(Firstname),
    write('Enter Last Name :'),tab(1),read(Lastname),
    write('Enter Course :'),tab(1),read(Course),
    write('Enter Year :'),tab(1),read(Year),
    write('Enter Section :'),tab(1),read(Section),
    write('Enter number of subjects: '),read(Subjects),
        
    nl,
    write('Student Profile:'),nl,nl,
    write('ID Number :'),tab(1),write(Idnumber),nl,
    write('Name :'),tab(1),write(Lastname),write(', '),write(Firstname), nl,
    write('Course :'),tab(1),write(Course), nl,
    write('Year and Section :'),tab(1),write(Year),write(' - '),write(Section), nl,
    write('Number of Subjects: '), write(Subjects), nl,
    Total is Subjects*5000,
    write('Total Payment for Subjects Taken: '), write(Total), nl,
    
    
    student_list(Idnumber,Firstname,Lastname,Course,Year,Section,Subjects,Total),nl,
    
    nl, write('Add New Student? [yes/no]: '), read(Y), answer2(Y).
    answer2(yes):- add.
    answer2(no):- main.
    
search:-
    nl, put_char('*'), put_char('S'), put_char('E'), put_char('A'), put_char('R'), put_char('C'), put_char('H'),
    put_char(' '),put_char('S'),put_char('T'), put_char('U'),put_char('D'), put_char('E'), put_char('N'),
    put_char('T'),put_char('*'), nl, nl,    
    
    write('Enter Id Number of Student: '),
    read(Input),nl,
    
        
    student_list(Input,Fn,Ln,Co,Ye,Sec,Sub,Tot),nl,
    
    write('Student Profile: '), nl,
    write('Name :'),tab(1),write(Ln),write(', '),write(Fn), nl,
    write('Course :'),tab(1),write(Co), nl,
    write('Year and Section :'),tab(1),write(Ye),write(' - '),write(Sec), nl,
    write('Number of Subjects: '), write(Sub), nl,
    write('Total Payment for Subjects Taken: '), write(Tot), nl,
        
    nl, write('Search Again? [yes/no]: '), read(Z), answer3(Z).
    answer3(yes):- search.
    answer3(no):- main.

您可以使用 assertz 将事实插入 Prolog 数据库:

assertz(student_list(Idnumber,Firstname,Lastname,Course,Year,Section,Subjects,Total))

(注意。如果您在源代码中放置任何 student_list(...) 示例,则可能需要在代码顶部使用指令 :- dynamic(student_list/8).)。