Displaying all the members of database in Prolog

孤街醉人 提交于 2020-02-06 08:06:07

问题


I'm trying to make a predicate "find_recipe" which fetches a list of ingredients based on the name of the recipe, given by the user.

I defined 3 recipes and am trying to first show a prompt for a user, let the user enter the recipe name, and then next few lines which are commented out should handle fetching the designated recipe, and lastly the line beginning with format will print out the result.

recipe('Makaronilaatikko', ['macaroni', 'potato', 'onion', 'cheese', 'milk', 'egg', 'minced meat']).
recipe('Curry rice', ['rice', 'curry powder', 'potato', 'onion', 'carrot', 'bacon']).
recipe('Sandwich', ['bread', 'onion', 'egg', 'bacon']).


find_recipe(Recipename, Result):-
    write(‘Enter the name of the recipe you want: ’), nl,
    read(Recipename),
    % go through the entire data of recipes defined
    % find the recipe whose name matches the use input
    format(‘Ingredients needed for ~w are ~w ~n’, [Recipename, Result]).

but I have no idea how the part

% go through the entire data of recipes defined
% find the recipe whose name matches the use input

should be implemented in Prolog, though I know I could write like

for(int i=0; i<recipeList.length;i++){
  Recipe result = new Recipe();
  if(recipeList[i].name == Recipename){
    result = recipeList[i]<
  }
  return result.ingredients;
}

if I were to write the same thing in a Java like language.

But how do I do this in Prolog?

And perhaps the recipes in my code above are defined in a wrong way in the first place?

[UPDATE]

This is the usage of find_recipe predicate I've been thinking of

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

find_recipe(Recipename, Result):-
    write(‘Enter the name of the recipe you want: ’), nl,
    read(Recipename),
    % go through the entire data of recipes defined
    % find the recipe whose name matches the use input
    format(‘Ingredients needed for ~w are ~w ~n’, [Recipename, Result]).

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

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

startas('customer'):-
    write('Logged in as customer'), nl,
    write('Let us help you find the ingredients you want!'), nl.
    write('Enter the name of the recipe you want:'),
    % somehow read the user's input, and then pass that to find_recipe predicate


回答1:


This is (part of) what makes Prolog so beautiful; proving

recipe(RecipeName,Result)

when RecipeName has been bound to a value will result in Result being bound to the ingredients for that recipe; Prolog does the searching for you.



来源:https://stackoverflow.com/questions/59986522/displaying-all-the-members-of-database-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!