如何在 prolog 过程中的最后一个参数之后插入附加参数?

发布于 2024-08-26 12:39:34 字数 201 浏览 10 评论 0原文

我是学习序言的新手,我想知道,如果我们有一些像这样的程序

father("Nic","Adam").  

,我想编写一个函数,它将为此添加新的值,

father("Nic","Adam","something"..) 

我该怎么做?使用列表?或者什么?

I am new to learning prolog, and I want to know, if we have some procedure like

father("Nic","Adam").  

and I want to write a function that it will add new value to this

father("Nic","Adam","something"..) 

how can I do this? Using list? Or what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

得不到的就毁灭 2024-09-02 12:39:34

快速回答:你不想这样做。

更长的答案: father/2 谓词有一定的含义,即对于 father(X,Y) X的父亲>Y。 father/3 谓词完全是另一回事。你想通过第三个论点实现什么目标?通常,您使用附加规则,这些规则从 father/2 谓词派生事物,甚至将其解析为 father/3 参数。

主要问题仍然是:第三个论点的目的是什么?例如,如果您希望您的解析基于相应的 father/2 谓词的存在而适用于某些特定的第三个参数,您可以执行 father(X, Y, 'something') : -father(X,Y) 如果您有相应的 father/2 事实,则会成功。

PS:一定要学习你的术语。在 Prolog 中,我们不谈论过程,也不编写函数。相反,我们有谓词事实规则、...

PPS:我不确定您正在使用哪种 Prolog 实现,但您可能想要使用 'something' 而不是 "something"。后者通常创建一个字符代码列表,而不是一个字符串:

?- X = 'some'.
X = some.

?- X = "some".
X = [115, 111, 109, 101].

Quick answer: You don't want to do that.

Longer answer: The father/2 predicate has a certain meaning, namely that for father(X,Y) X is the father of Y. A father/3 predicate is a different thing altogether. What do you want to achieve with that third argument? Normally, you use additional rules, which derive things from the father/2 predicate, or even resolve it to a father/3 argument.

The main question remains: what's the purpose of the third argument? If you want your resolution to work for certain specific 3rd arguments based on the existance of a corresponding father/2 predicate for example, you could do father(X, Y, 'something') :- father(X,Y) which will succeed if you have a corresponding father/2 fact.

PS: Do learn your terminology. In Prolog we don't speak of procedures and we don't write functions. Instead we have predicates, facts, rules, ...

PPS: I am not sure which Prolog implementation you are using, but you might want to use 'something' instead of "something". The latter usually creates a list of character codes, not a string:

?- X = 'some'.
X = some.

?- X = "some".
X = [115, 111, 109, 101].
我家小可爱 2024-09-02 12:39:34

简单地写

father(nic, adam). 

为 As 谓词就已经定义了它。这就像陈述一个事实:您声明 father(nic, adam)true,然后您可以使用这些预期结果执行以下命令:

?- father(nic, adam).
Yes

?- father(nic, X).
X = adam

Simply writing

father(nic, adam). 

As a predicate already defines it. It is like stating a fact: you declare that father(nic, adam) is true, then you can execute the following with these expected results :

?- father(nic, adam).
Yes

?- father(nic, X).
X = adam
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文