gpt4 book ai didi

prolog - 在 Prolog 中编写函数

转载 作者:行者123 更新时间:2023-12-01 11:22:12 24 4
gpt4 key购买 nike

我正在使用 SWI Prolog。我想定义一个添加函数:add(X, Y)它返回 X 和 Y 的总和。但是,我不知道如何在 Prolog 中定义函数。我尝试使用谓词这样做:

add(X, Y, Z) :- Z is X+Y.

但是在执行形式为 add(2, 3, X) 的查询时这给出了一个错误说:

ERROR: toplevel: Undefined procedure: add/3 (DWIM could not correct goal)



另外,我无法理解 :- 之间的区别和 :=在编写规则时。我在某处读到 :-用于定义谓词而 :=用于定义函数。我不确定这是否正确。我尝试使用 :=用于定义函数但它不起作用。

最佳答案

将自己放在与 prolog 文件相同的目录中,键入以下内容以加载和编译源文件 function.pl :

[function].

现在测试运行(使用您在文件 function.pl 中发布的确切代码):
?- add(2,3,X).
X = 5.

使用了 Swi-prolog v 6.6.4。

Also, I cannot understand the difference between :- and := while writing rules. I read somewhere that :- is used to define predicates while := is used to define functions. I am not sure if this is correct. I tried using := for defining functions but it doesn't work.



逻辑程序是一组公理或规则(又名谓词),定义对象之间的关系,不使用显式函数的概念,但由于函数实际上只是从输入集到输出集的映射,您可以将其建模为一个关系,就像您对 add/3 所做的一样关系。
:-可以理解为逻辑含义(实际上我认为该符号应该看起来像向后箭头)
:=您所指的运算符我什至在 swi prolog 文档中都找不到: search results for := .如果您可以链接到找到有关它的信息的位置,那就太好了。

关于prolog - 在 Prolog 中编写函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40821641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com