Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if else if else clause in prolog similar to C/C++

Tags:

prolog

In c language i have something like :

if(cond1)
{}
else if(cond2)
{}
else
{}

how is this possible in Prolog?

like image 445
Vijay Avatar asked Apr 12 '12 11:04

Vijay


People also ask

Is there if else in Prolog?

“prolog if” is a statement to support conditions of the application's data and its operations. It is a conditional function to display the required condition of the prolog programming language. It is a function to prove the true and false condition and operation of the given data or values using a programming language.

How do you write an if else condition in Prolog?

% If-Then-Else statement gt(X,Y) :- X >= Y,write('X is greater or equal'). gt(X,Y) :- X < Y,write('X is smaller'). % If-Elif-Else statement gte(X,Y) :- X > Y,write('X is greater'). gte(X,Y) :- X =:= Y,write('X and Y are same').

What does -> mean in Prolog?

The arrow in Prolog does not correspond to material implication in first-order logic. It's a ternary "if-then-else" operator with an optional alternative. Because of the way it's implemented in Prolog syntax, (true -> false) ; true.

How do you create an OR statement in Prolog?

Performing an "or" in Prolog can also be done with the "disjunct" operator or semi-colon: registered(X, Y) :- X = ct101; X = ct102; X = ct103. Save this answer.


2 Answers

(   If1 -> Then1
;   If2 -> Then2
;   ...
;   otherwise
).

Note that if-then-else is only necessary if you cannot express the different conditions by pattern matching in different clauses. Everything that can be expressed by pattern matching should be expressed by pattern matching, as this typically leads to more general and also more efficient code.

like image 71
mat Avatar answered Oct 04 '22 19:10

mat


(cond1 ->
    consequent1
; cond2 ->
    consequent2
;
    alternative
)

For the record, this is called a conditional.

like image 20
Fred Foo Avatar answered Oct 04 '22 19:10

Fred Foo