查找序言程序给出错误结果的查询 [英] Finding query for which a prolog program gives incorrect result

查看:84
本文介绍了查找序言程序给出错误结果的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此Prolog程序将第三个参数定义为前两个数字参数的最大值:

This Prolog program de fines the third argument to be the maximum value of the fi rst two numeric arguments:

max(X, Y, X) :- X >= Y, !.
max(X, Y, Y).

我认为该程序可以正常运行.但有人告诉我它可以给出错误的结果.你能说出何时和为什么吗?

I think that this program works just fine. But I am told that it can give incorrect result. Can you tell when and why?

推荐答案

这是一个教科书示例.

?- max(5,1,1).
true.

家庭作业:为什么程序出错?我们如何使程序正确?

Homework: Why is the program wrong? How do we make the program correct?

编辑

max(X, Y, X) :- X >= Y, !.
max(X, Y, Y).

我们的意图是说:

如果X大于Y,则然后 Max为X.否则,Max必须为Y.

If X is greater than Y, then Max is X. Otherwise, Max must be Y.

相反,说的是:

当第一个和第三个参数(X和Max)可以统一且X大于Y时,则成功.否则,如果第二个和第三个参数(Y和Max)可以统一,则成功.

When the first and third arguments (X and Max) can be unified, and X is greater than Y, succeed. Otherwise, if the second and third arguments (Y and Max) can be unified, succeed.

出现明显的问题,然后第一个和第三个参数不能统一,但是第二个和第三个参数可以统一.

The obvious problem arises then the first and third arguments cannot be unified, but the second and the third can.

相反:

max(X, Y, X) :- X >= Y.
max(X, Y, Y) :- X < Y.

max(X, Y, Max) :- X >= Y, !, Max = X.
max(_, Max, Max).

这篇关于查找序言程序给出错误结果的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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