Отображение всех членов базы данных в Прологе - PullRequest
0 голосов
/ 30 января 2020

Я пытаюсь создать предикат "find_recipe", который выбирает список ингредиентов на основе названия рецепта, данного пользователем.

Я определил 3 рецепта и пытаюсь сначала показать подсказку для пользователя, дать пользователю возможность ввести имя рецепта, а затем следующие несколько закомментированных строк должны обработать выборку назначенного рецепта и, наконец, строку начиная с format распечатает результат.

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]).

но я понятия не имею, как часть

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

должна быть реализована в Прологе, хотя я знаю, что могу написать как

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

, если я должны были написать то же самое на Java подобном языке.

Но как мне это сделать в Прологе?

А, может быть, рецепты в моем коде выше определены неправильно во-первых?

[ОБНОВЛЕНИЕ]

Это использование предиката find_recipe, о котором я думал

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 Ответ

0 голосов
/ 30 января 2020

Это (часть) того, что делает Пролог таким красивым; Доказательство

recipe(RecipeName,Result)

, когда RecipeName привязано к значению, приведет к тому, что Result будет привязано к ингредиентам для этого рецепта; Пролог ищет тебя.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...