我有这个任意函数,我需要用不同的变量多次调用它。
顺便说一句,这是 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/