在Prolog中显示数据库的所有成员 [英] Displaying all the members of database in Prolog

查看:29
本文介绍了在Prolog中显示数据库的所有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个谓词find_recipe",它根据用户给出的食谱名称获取成分列表.

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.

我定义了 3 个菜谱,我试图首先向用户显示一个提示,让用户输入菜谱名称,然后被注释掉的接下来几行应该处理获取指定的菜谱,最后以 <开头的行code>format 将打印出结果.

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

但我不知道这部分如何

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

应该在 Prolog 中实现,虽然我知道我可以像

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;
}

如果我用类似 Java 的语言编写同样的东西.

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

但是我如何在 Prolog 中做到这一点?

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?

[更新]

这是我一直在想的find_recipe谓词的用法

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

推荐答案

这是(部分)使 Prolog 如此美丽的原因;证明

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

recipe(RecipeName,Result)

RecipeName 绑定到一个值时,将导致 Result 绑定到该配方的成分;Prolog 为您进行搜索.

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.

这篇关于在Prolog中显示数据库的所有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆