Is there a way to do an if in prolog, e.g. if a variable is 0, then to do some actions (write text to the terminal). An else isn't even needed, but I can't find any documentation of if.
‘if’ in prolog
iso-prologlogical-purityprolog
iso-prologlogical-purityprolog
Is there a way to do an if in prolog, e.g. if a variable is 0, then to do some actions (write text to the terminal). An else isn't even needed, but I can't find any documentation of if.
Best Answer
Yes, there is such a control construct in ISO Prolog, called
->
. You use it like this:Here is an example that uses a chain of else-if-clauses:
Note that if you omit the else-clause, the condition failing will mean that the whole if-statement will fail. Therefore, I recommend always including the else-clause (even if it is just
true
).