Search code examples
functionprologargumentsvisual-prolog

Visual Prolog - take some function as argument of another function


I'm a new in Visual Prolog, and as I understand, this language seems as functional. And so on, I have a question: can we do smth like this (and if 'can' then 'how'):

func1(X, Y, Func2) :-
  R = somefunc(X,Y),
    if R = "yes", ! then
       Func2  %here I want to call function with name, which is in variable 'Func2'
    else
        stdIO::write("End of work"),
        stdIO::nl,
        fail
    end if.

The cause of this question - I need to call different functions in same way, with checking answer from console. and if it wasn't 'yes' - stop running program.


Solution

  • First of all, Prolog doesn't have functions, those things are predicates. The difference matter a lot, since there can be multiple ways to satisfy (prove) a predicate is true, but there is typically only one way to interpret a function.

    I've never used Visual Prolog, but what you are asking can be accomplished in most flavors of Prolog I've seen using =../2 and call/1 as follows:

    Func2WithArgs =.. [Func2, Arg1, Arg2],
    call(Func2WithArgs).
    

    for instance:

    X = writeln, Call =.. [X, 'Hellow World'], call(Call).