Прямо сейчас я делаю предикат "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;
}
Если вы пишете код Prolog, соответствующий Java -подобному коду, написанному выше, как он выглядит?
[ОБНОВЛЕНО]
Я обновил предикат find_book
как
find_book(Booktitle, Result):-
write(‘Enter the book title: ’),
read(Booktitle),
book(Booktitle, _),
format(‘The author of ~w is ~w ~n’, [Booktitle, Result]).
, а предикат 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).