prolog 将函数作为变量传递,如何添加参数?

标签 prolog declarative

我有这个任意函数,我需要用不同的变量多次调用它。
顺便说一句,这是 SWI-Prolog

perform(V1,V2,V3,Function,Result):-
    % 
    % do little stuf.
    %
    Function(Arg1,Arg2,Result).

这给出了语法错误。

但是将函数作为变量传递而不添加参数可以正常工作,如下面的代码所示:
perform(Function):-
    Function.

sayHello:-
    write('hello').

:-perform(sayHello).

那么如何给可变函数添加参数呢?

最佳答案

特别是在 SWI-Prolog 中,您可以使用 call .引用手册:

call(:Goal, +ExtraArg1, ...)

Append ExtraArg1, ExtraArg2, ... to the argument list of Goal and call the result. For example, call(plus(1), 2, X) will call plus(1, 2, X), binding X to 3. The call/[2..] construct is handled by the compiler. The predicates call/[2-8] are defined as real (meta-)predicates and are available to inspection through current_predicate/1, predicate_property/2, etc. Higher arities are handled by the compiler and runtime system, but the predicates are not accessible for inspection.



其中加号表示 argument must be fully instantiated to a term that satisfies the required argument type , 冒号表示 agument is a meta-argument (这也意味着“+”)。

关于prolog 将函数作为变量传递,如何添加参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10563818/

相关文章:

prolog - if else if prolog中的else子句类似于C/C++

list - 序言; if 和(停止)递归

list - 反向/回文的递归Prolog谓词

list - 这个程序的声明性解释说明一个元素是否属于一个列表

kubernetes - 'kubectl apply' 和 'kubectl create' 的区别

list - 只保留一份重复的元素

prolog - Prolog 3 路比较中的关系运算符符号

python - SQLAlchemy中如何设置复杂条件复合外键

java - Jenkinsfile (Declarative Pipeline) for Java/maven/Spring Boot

C++ 声明式解析序列化