7

这是我的课堂问题之一。

问题

我能够用一堆 if-else 创建自己的 Prolog 程序,但有人告诉我,我的程序不是完全声明性的,因为它是 Prolog 的基本原则之一。

这是我的代码

%start with :- go.
is_spicy :-
  write('Do you want spicy? (yes/no)'),
  read(Preference),
  ( Preference == yes -> recommend("Curry"); recommend("Kurma") ).

is_fry :-
  write('Do you want fry food? (yes/no)'),
  read(Preference),
  ( Preference == yes -> recommend("StirFry"); recommend("Chicken") ).

is_chili :-
  write('Do you want chili? (yes/no)'),
  read(Preference),
  ( Preference == yes -> recommend("Sambal"); recommend("Singgang") ).

recommend(Food) :- write('We recommend you '), write(Food).

go :-
  write('Which food type do you prefer? (indian, chinese, malay): '),
  read(FoodType),
  (
    FoodType == indian -> is_spicy;
    FoodType == chinese -> is_fry;
    FoodType == malay -> is_chili;
    writeln('Please try again')
  ).

有人对如何使其“完全声明性”有任何想法吗?

4

2 回答 2

5

如果您解决代码中的冗余问题,从逻辑中提取数据,那么您最终会得到更多的声明性代码。

因此,对数据结构进行编码,并提供一个能够提出问题并推断结果的“解释器”。

例如

dt(food_type,
    [indian  -> dt(spicy, [ y -> curry,   n -> curma ])
    ,chinese -> dt(fry,   [ y -> stirFry, n -> chicken ])
    ,malay   -> dt(chili, [ y -> sambal,  n -> singgang ])
    ]).

interpreter(dt(About, Choices), Choice) :-
   % present a menu for choices
   % recurse on selected path

% when it reach a leaf, just unify
interpreter(Choice, Choice).

您可能希望专门针对 y/n 选择菜单,但这取决于您

编辑

呈现菜单并接受选择需要额外的逻辑编程,例如:

solve(Choice) :-
    dt(About, Choices),
    interpreter(dt(About, Choices), Choice).

% present a menu for choices
% recurse on selected path
interpreter(dt(About, Choices), Choice) :-
  ask_user(About, Choices, ChoiceAbout),
  interpreter(ChoiceAbout, Choice).

% when it reach a leaf, just unify
interpreter(Choice, Choice).

ask_user(About, Choices, Choice) :-
  format('your choice about ~w ?~n', [About]), % show user the context of choice
  forall(member(C->_,Choices), format('   ~w~n', [C])),
  read(U),
  memberchk(U->Choice, Choices).

% note: if memberchk above fails (user doesn't input a correct choice)
% you should provide an alternate ask_user here, otherwise ...
% just see what the code does
%

示例会话:

% /home/carlo/Desktop/pl/choices compiled into choices 0.00 sec, 0 clauses

?- solve(C).

your choice about food_type ?
   indian
   chinese
   malay
|: chinese.

your choice about fry ?
   y
   n
|: n.

C = chicken .
于 2016-12-31T13:04:20.703 回答
3

CapelliC给出了一个更精致的答案,我喜欢这个答案,因为我也从他的答案中学到了一些东西。

这是一个更简单的版本,应该有助于连接点。

诀窍是将期望的最终结果视为一组单独的规则,例如food(indian,spicy),然后如何达到它们。一旦我将答案从是/否更改为变量的值,其余的就下坡了。显然可以使用是/否,但这只需要额外的编码。

food(indian) :-
  write('Do you want spicy? (spicy/not_spicy)'),
  read(Preference),
  food(indian,Preference).

food(chinese) :-
  write('Do you want fry food? (fry/not_fry)'),
  read(Preference),
  food(chinese,Preference).

food(malay) :-
  write('Do you want chili? (chili/not_chili)'),
  read(Preference),
  food(malay,Preference).

food(indian,spicy) :- recommend("Curry").
food(indian,not_spicy) :- recommend("Kurma").

food(chinese,fry) :- recommend("StirFry").
food(chinese,not_fry) :- recommend("Chicken").

food(malay,chili) :- recommend("Sambal").
food(malay,not_chili) :- recommend("Singgang").

recommend(Food) :- write('We recommend you '), write(Food).

go :-
  write('Which food type do you prefer? (indian, chinese, malay): '),
  read(FoodType),
  food(FoodType).
于 2016-12-31T13:24:52.180 回答