Diogo Santos
Diogo Santos

Reputation: 830

Call different predicate if variable is not instantiated

Having this snippet

foo :- getVar(X).

How can I do a really simple thing such as: "if the variable X comes out of getVar instatiated, call bar/1 else call baz/1

Thanks!

Upvotes: 1

Views: 36

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

You can use an if-then-else control construct and the nonvar/1 standard built-in predicate. For example:

foo :-
    get_var(X),
    (   nonvar(X) ->
        bar(X)
    ;   baz(X)
    ).

Upvotes: 2

Related Questions