如何在Prolog中询问用户选择?



我刚刚开始学习Prolog。我正在从水果样本中练习。但我无法创建允许用户按数字选择的结构。我在互联网上看到的例子对水果样品无效。

当我在SWISH-Prolog上运行以下代码时,我必须一一写入列表中的值。 如何选择"1,2,3,4"而不是红色,橙色,黄色,绿色,紫色,桃色?

错误代码块:

menuask(Attr, Val, List) :- 
write('What is the value for '), write(Attr), write('? '), nl,
write (1. red), nl,
write (2. orange), nl,
read(Ans),
check_val(Ans, Attr, Val, List),
asserta(known(yes, Attr, Ans)),
Ans == Val.

在"菜单询问"中给出一个错误。我试过了。

记录所有代码:

:- dynamic(known/3).
:- discontiguous menuask/3.
:- discontiguous ask/2.
% 
% % Data: fruit(X) :- attributes(Y)
fruit(banana) :- colour(yellow), shape(crescent).
fruit(apple) :- (colour(green); colour(red)), shape(sphere), stem(yes).
fruit(lemon) :- colour(yellow), (shape(sphere);shape('tapered sphere')), acidic(yes).
fruit(lime) :- colour(green), shape(sphere), acidic(yes).
fruit(pear) :- colour(green), shape('tapered sphere').
fruit(plum) :- colour(purple), shape(sphere), stone(yes).
fruit(grape) :- (colour(purple);colour(green)), shape(sphere).
fruit(orange) :- colour(orange), shape(sphere).
fruit(satsuma) :- colour(orange), shape('flat sphere').
fruit(peach) :- colour(peach).
fruit(rhubarb) :- (colour(red); colour(green)), shape(stick).
fruit(cherry) :- colour(red), shape(sphere), stem(yes), stone(yes).
% Expert recogniser
% Asks
colour(X) :- menuask(colour, X, [red, orange, yellow, green, purple, peach]).
shape(X) :- menuask(shape, X, [sphere, crescent, 'tapered sphere', 'flat sphere', stick]).
acidic(X) :- ask(acidic, X).
stem(X) :- ask(stem, X).
stone(X) :- ask(stone, X).
% Remember what I've been told is correct
ask(Attr, Val) :- known(yes, Attr, Val), !.
menuask(Attr, Val, _) :- known(yes, Attr, Val), !.
% % Remember what I've been told is wrong
ask(Attr, Val) :- known(_, Attr, Val), !, fail.
menuask(Attr, Val, _) :- known(_, Attr, Val), !, fail.
% Remember when I've been told an attribute has a different value
ask(Attr, Val) :- known(yes, Attr, V), V == Val, !, fail.
menuask(Attr, Val, _) :- known(yes, Attr, V), V == Val, !, fail.
% % I don't know this, better ask!
ask(Attr, Val) :- write(Attr:Val), write('? '), read(Ans), asserta(known(Ans, Attr, Val)), Ans == yes.
menuask(Attr, Val, List) :- 
write('What is the value for '), write(Attr), write('? '), nl,
write(List), nl,
read(Ans),
check_val(Ans, Attr, Val, List),
asserta(known(yes, Attr, Ans)),
Ans == Val.
check_val(Ans, _, _, List) :- member(Ans, List), !.
check_val(Ans, Attr, Val, List) :- 
write(Ans), write(' is not a known answer, please try again.'), nl,
menuask(Attr, Val, List).
go :- fruit(Fruit), write('The fruit is '), write(Fruit), nl.

我不完全清楚问题的重点,但也许这会有所帮助。下面是如何获取与数字输入对应的名称的简短示例:

fruit(1, apple).
fruit(2, pear).
fruit(3, orange).
read_fruit(FruitName) :-
repeat,
write('Please select a fruit:'), nl,
write_fruit_list,
read(FruitNumber),
(   fruit(FruitNumber, FruitName)
->  write('You selected: '), write(FruitName), nl, !
;   write('Not a valid choice, try again...'), nl, fail
).
write_fruit_list :-
fruit(N, Name),
write(N), write('. '), write(Name), nl,
fail.
write_fruit_list.

以下是使用它时的外观:

2 ?- read_fruit(F).
Please select a fruit:
1. apple
2. pear
3. orange
|: 4.
Not a valid choice, try again...
Please select a fruit:
1. apple
2. pear
3. orange
|: 3.
You selected: orange
F = orange.
3 ?-

如果您希望切口不断要求水果输入,您可以删除切口。

相关内容

  • 没有找到相关文章

最新更新