Prolog 调用自己定义的谓词时如何访问数据库成员

Prolog 调用自己定义的谓词时如何访问数据库成员,prolog,Prolog,现在我正在做一个“find_book”谓词,通过它,您可以通过用户输入找到指定的书籍 book('C for Dummies', 'Chris Smith', 2000). book('C++ for Dummies', 'Chris Smith', 2002). book('Java for Dummies', 'Jason Rash', 1995). book('JavaScript for Dummies', 'Jason Rash', 2005). book('Prolog for Du

现在我正在做一个“find_book”谓词,通过它,您可以通过用户输入找到指定的书籍

book('C for Dummies', 'Chris Smith', 2000).
book('C++ for Dummies', 'Chris Smith', 2002).
book('Java for Dummies', 'Jason Rash', 1995).
book('JavaScript for Dummies', 'Jason Rash', 2005).
book('Prolog for Dummies', 'Pete Fagan', 1990).


main():-
    chooseusertype(Usertype),
    startas(Usertype),
    find_book(Booktitle).  % somehow read the user's input, and then pass that to find_book predicate

find_book(Booktitle, Result):-
    write(‘Enter the book title: ’), nl,
    read(Booktitle),
    % go through the entire data of books defined
    % find the book whose name matches the user input
    format(‘The author of ~w is ~w ~n’, [Booktitle, Result]).

chooseusertype(X):-
    write('Log in as a librarian or guest?: '),
    read(X),
    format('Your log in type: ~w', [X]).

startas('librarian'):-
    write('Logged in as librarian'), nl,
    write('Any update on the shelves?').

startas('guest'):-
    write('Logged in as guest'), nl,
    write('Let us help you find the book you are looking for!'), nl.
    write('Enter the book title:'),
    % somehow read the user's input, and then pass that to find_book predicate
我不知道如何实现这个部分

% go through the entire data of books defined
% find the book whose name matches the user input
,虽然我知道,在类似Java的语言中,您可以像

for(int i=0; i<bookList.length;i++){
  Book result = new Book();
  if(bookList[i].name == Booktitle){
    result = bookList[i];
  }
  return result.author;
}
startas('guest')
谓词为

startas('guest'):-
    write('Logged in as guest'), nl,
    write('Let us help you find the book you are looking for!'), 
    find_book(Booktitle).


相关问题:搜索是Prolog为您所做的事情,您无需明确说明。如果用户输入的是变量
Booktitle
,则
book(Booktitle,,,)
足以检索它(或者您可以同时获取作者和年份,这并不重要)。@DanielLyons谢谢您的回答。但是如何保存
book(bookstitle,)
的搜索结果的返回值呢?我更新了我原来的帖子,现在你可以看到代码是如何更改的。更新后,输出类似于
“C for Dummies的作者是_2304”
。我知道
\u 2304
部分应该是返回值,但我不知道如何将其嵌入
格式
语句中。
book(Booktitle,u),
应该是
book(Booktitle,u,u),
谓词(call)中的数字参数需要与事实中的项数匹配。这应该是可行的<代码>书籍(书名,作者,_Year),格式(~w的作者是~w~n',[bookstitle,Author])相关问题:搜索是Prolog为您做的事情,您无需明确说明。如果用户输入的是变量
Booktitle
,则
book(Booktitle,,,)
足以检索它(或者您可以同时获取作者和年份,这并不重要)。@DanielLyons谢谢您的回答。但是如何保存
book(bookstitle,)
的搜索结果的返回值呢?我更新了我原来的帖子,现在你可以看到代码是如何更改的。更新后,输出类似于
“C for Dummies的作者是_2304”
。我知道
\u 2304
部分应该是返回值,但我不知道如何将其嵌入
格式
语句中。
book(Booktitle,u),
应该是
book(Booktitle,u,u),
谓词(call)中的数字参数需要与事实中的项数匹配。这应该是可行的<代码>书籍(书名,作者,_年),格式(~w的作者是~w~n',[Booktitle,作者])
startas('guest'):-
    write('Logged in as guest'), nl,
    write('Let us help you find the book you are looking for!'), 
    find_book(Booktitle).