->
Entity: predicate and function

Usage:
A1, ... , An -> B1, ... , Bm else C1, ... . Ck
A1, ... , An -> B1, ... , Bm
R is (G1, ... , Gn -> X else Y)

Predicate:

Usage:
A1, ... , An -> B1, ... , Bm else C1, ... . Ck
A1, ... , An -> B1, ... , Bm

The predicate -> (if-then-else) executes first the left goal A1, ... , An and if it succeeds then executes the middle goal - B1, ... , Bm , if it fails then executes the right goal - C1, ... , Ck . If the right goal is missing then this is the same as if it was the empty goal. In this predicate you can write ; instead of else . This is for compatibility with other Prolog compilers.

If you write:

?-a->b->c;d;e.

then the last ; will be accepted not as a part of if-then-else but as an operator or .

If you want to create more complicated expression with many if-then-else operators in it - then use round brackets - ( and ) .

Like this:

?-a-> (b->c;d);e.

Be careful, in this case, to leave a space between -> and ; and ( , otherwise you can obtain the error No sense .

Look also at the function -> (if-then-else) .

Example:

?-
  yes_no("1","",?) ->
    (yes_no("2","",?) ->
      write(a), nl
    else
      write(b), nl
    )
  else (yes_no("3","",?) ->
    write(c), nl
  else
    write(d), nl
  ).

Function:

Usage:
R is (G1, ... , Gn -> X else Y)

If you use -> as a function then G1, ... , Gn have to be an atomic goals and X and Y have to be any expressions. If the goal G1, ... , Gn succeeds then this function returns the value of the expression X, otherwise returns the value of the expression Y.

Example:

?- write(yes_no("Say","one or two",?)-> 1 else 2).