SWI Prolog 中是否有类似匿名谓词的东西? [英] Is there something like anonymous predicates in SWI Prolog?

查看:30
本文介绍了SWI Prolog 中是否有类似匿名谓词的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 SWI Prolog 中定义一个匿名谓词,将它绑定到一个变量,然后再调用它吗?像这样:

Can I define an anonymous predicate in SWI Prolog, bind it to a variable, and call it later? Something like this:

?- F = {(X, Y) :- Y is 2 * X}, call(F, 2.0, Y).

推荐答案

或者,在 SWI-Prolog 中,您可以使用 图书馆(yall).它可以自动加载,因此您无需导入任何内容:

Alternatively, in SWI-Prolog, you can use library(yall). It can be autoloaded so you don't need to import anything:

?- F = [X, Y]>>( Y is 2*X ), call(F, 2.0, Y).
F = [X, 4.0]>>(4.0 is 2*X),
Y = 4.0.

认为在一般情况下,最好为调用 lambda 的结果使用一个新变量:

I think that in the general case it would be better to use a new variable for the result of calling the lambda:

?- F = [X, Y]>>( Y is 2*X ), call(F, 2.0, R).
F = [X, Y]>>(Y is 2*X),
R = 4.0.

这篇关于SWI Prolog 中是否有类似匿名谓词的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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