Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do a Nested if-else statement in Prolog?

Tags:

People also ask

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').

How do you check multiple conditions in Prolog?

In Prolog, use ";" for or and "," for and. gothrough([H|T], B, C):- ( (T == [] ; H == 'then') %if either the tail is an empty list or if H == "then", do the following% -> append(H,B,B), outputs(B,C) ; append(H,B,B), gothrough(T, B, C) %else% ).

What does #= mean in Prolog?

I looked this up on the SWI prolog website where they defined it as. The arithmetic expression X equals Y. When reasoning over integers, replace is/2 by #=/2 to obtain more general relations.

What is S () in Prolog?

It is Prolog-implementation specific. It refers to a successor-predicate, see this for some more info. Follow this answer to receive notifications.


If I have this function:

min(List1, List2, Output) :-
   length(List1, N),
   length(List2, M),
   (   N < M ->
       Output = 'true'
   ;   Output = 'false'
   ).

but what if I wanted to also check if N == M? Maybe like this:

min(List1, List2, Output) :-
   length(List1, N),
   length(List2, M),
   (   N < M ->
       Output = 'true'
   ;   (  N = M ->
          Output = 'equal'
       ;  Output = 'other'
       )
   ).

Doesn't seem to work.