Prolog passing a function as a variable, how to add arguments?

I have this arbitrary function that I need to call many times with different variables. By the way, this is SWI-Prolog

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

This gives a syntax error.

But passing the function as a variable without adding arguments works fine, as in the following code:

 perform(Function):- Function. sayHello:- write('hello'). :-perform(sayHello). 

So how to add arguments to a variable?

+4
source share
1 answer

In particular, in SWI-Prolog you can use call . Guideline Note:

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

Add ExtraArg1, ExtraArg2, ... to the target argument list and call the result. For example, a call (plus (1), 2, X) will call a plus (1, 2, X), linking X to 3. Call / [2 ..] The construction is processed by the compiler. Call / [2-8] predicates are defined as real (meta) predicates and are available for checking through current_predicate / 1, predicate_property / 2, etc. arities are handled by the compiler and the runtime system, but the predicates are not validated.

where plus indicates that argument must be fully instantiated to a term that satisfies the required argument type , and a colon indicates that agument is a meta-argument (this also means "+").

+8
source

Source: https://habr.com/ru/post/1412145/


All Articles