1

Right now I'm making a "find_book" predicate, with which you can find the designated book by user input.

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

and I don't know how to implement the part

% go through the entire data of books defined
% find the book whose name matches the user input

, though I know that in a Java-like language you could write like

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

If you write Prolog code that corresponds to the Java-like code written above, how does it look like?

[UPDATED]

I updated the predicate find_book as

find_book(Booktitle, Result):-
    write(‘Enter the book title: ’),
    read(Booktitle),
    book(Booktitle, _),
    format(‘The author of ~w is ~w ~n’, [Booktitle, Result]).

, and startas('guest') predicate as

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

  • Related question: [Displaying all the members of database in Prolog](https://stackoverflow.com/q/59986522/1243762) – Guy Coder Jan 30 '20 at 15:44
  • The search is something Prolog does for you without you having to be explicit. If the user input is in a variable `Booktitle`, then `book(Booktitle, _, _)` is sufficient to retrieve it (or you could get the author and year at the same time, it doesn't matter). – Daniel Lyons Jan 30 '20 at 15:49
  • @DanielLyons Thank you for the answer. But how do you save the return value of the search result of ```book(Booktitle, _)```? I updated my original post and now you can see how the code has been changed. With this updated, the output becomes like ```"The author of C for Dummies is _2304"```. I know the ```_2304``` part should be the returned value, but I have no idea how to embed that into the ```format``` statement. – furitetepporaa Jan 30 '20 at 18:17
  • `book(Booktitle, _),` should be `book(Booktitle, _, _),` the number arguments in the predicate (call) needs to match the number of items in the fact. – Guy Coder Jan 30 '20 at 18:39
  • 1
    This should work. `book(Booktitle, Author, _Year),format(‘The author of ~w is ~w ~n’, [Booktitle,Author])` – Guy Coder Jan 30 '20 at 18:40
  • `_Year` is an [anonymous variable](https://stackoverflow.com/q/14238492/1243762). It is typically used as `_` but can be used with a word as a mnemonic to remember what would go in that place if used. – Guy Coder Jan 30 '20 at 18:43
  • If you are using SWI-Prolog and prefer a discussion type format instead one question one answer format, see SWI-Prolog [forum](https://swi-prolog.discourse.group/) – Guy Coder Jan 30 '20 at 18:48
  • `Prolog for Dummies` So glad it does not exist. – Guy Coder Jan 30 '20 at 19:04

0 Answers0