Visual Prolog - take some function as argument of another function

245 views Asked by At

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.

2

There are 2 answers

0
Christian Fritz On BEST ANSWER

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).
0
Thomas Linder Puls On

The code seems correct except that you need parentheses when invoking the function. i.e. you must write Func2() rather than Func2.

func1(X, Y, Func2) :-
   R = somefunc(X,Y),
   if R = "yes", ! then
       Func2()  % parentheses here
    else
       stdio::write("End of work\n"),
       fail
    end if.

However if func1 and Func2 are indeed functions you need to deal with the return value:

func1(X, Y, Func2) = Result :-
   R = somefunc(X,Y),
   if R = "yes", ! then
       Result = Func2()
    else
       stdio::write("End of work\n"),
       fail % No result when failing
    end if.

Also notice that there is a dedicated Visual Prolog forum: http://discuss.visual-prolog.com