确定!最后的Prolog问题很长一段时间!!
我正在尝试选择一个随机选择的响应,但我似乎只是从我的响应表中选择第一个(参见代码)
我确信它已经完成了Prologs“findall”和“随机”但是如何?
pick_response(Sent, R) :-
response(Sent, R), !.
pick_response(_,R) :-
punt(R),!.
答案 0 :(得分:3)
使用findall/3
和random/3
执行此操作的一种方法是:
% Responses for sentence 'sentence'
response(sentence, first).
response(sentence, second).
response(sentence, third).
% 1. Generate a list of all responses
% 2. Generate a random integer
% 3. Pick the response with the index of the integer from the list
random_response(Sentence, RandomResponse) :-
findall(Response, response(Sentence, Response), List),
length(List, Len),
random(0, Len, RandomNumber),
nth0(RandomNumber, List, RandomResponse).
用法:
?- random_response(sentence, RandomResponse).
RandomResponse = third.
?- random_response(sentence, RandomResponse).
RandomResponse = first.
?- random_response(sentence, RandomResponse).
RandomResponse = second.
?- random_response(sentence, RandomResponse).
RandomResponse = second.
?- random_response(sentence, RandomResponse).
RandomResponse = second.
?- random_response(sentence, RandomResponse).
RandomResponse = third.
答案 1 :(得分:0)
你的问题是削减。我假设response/2
会在回溯时生成所有可能的回复,并且您希望能够在回溯上逐步执行它们并选择您想要的回复。如果没有合适的回复,则punt/1
将生成回复。
如果是这样,那么第一个子句中的剪切将停止在pick_response
和response
中回溯,因此您将只看到第一个解决方案。如果punt/1
生成解决方案o回溯,那么你也会遇到同样的问题,但是如果它只生成一个解决方案那么第二个就没有了。
这会将实际选择的响应移到pick_response
之外,然后真正成为generate_response
。
这是你的意图吗?