It is a bit misleading to talk about âor statementsâ in Prolog, I think. It is not an âor statementâ because it is not a statement and it isnât an âorâ in the widely accepted use of both these terms/concepts in programming.
Just repeating @jan now (nothing of substance coming from me).
This construct:
( condition
-> then
; else
)
is about as close as you can get to an âif-then-elseâ using conventional Prolog.
This is another way to write the same, wrapping it in a predicate:
foo :-
condition,
!,
then.
foo :-
else.
It starts getting confusing really fast though.
condition on its own, outside of those constructs, might leave choice points (and maybe succeed multiple times), and there is no obvious analogy to those in a programming language like C or Java.
condition might also have a side effect. In C at least there are idioms where the condition has a side effect, on purpose, like the good old
while ((c = getchar()) != EOF)
⌠but with Prolog, having a condition with a side effect is a code smell?
PS: the reason why I used to think of this construct as a âguardâ:
foo :- guard_a, !, a.
foo :- guard_b, !, b.
...
foo :- default.
is that I had recently read in a hurry some stuff about Erlang. There is something very similar in Erlang and there are guards. However! if memory serves correctly, you cannot use just anything as a guard in Erlang. Translating this to Prolog, it can only be side-effect free, and not instantiate its arguments any further! While in Prolog, you can have anything you want in the condition part, with varying levels of surprise as a result.