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

前端 未结 5 804
说谎
说谎 2021-01-18 14:53

In c language i have something like :

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

how is this possible in Prolog?

5条回答
  •  無奈伤痛
    2021-01-18 15:01

    if_then_else(Condition,Then,Else) :- Condition, !, Then.
    if_then_else(Condition,Then,Else) :- Else.
    

    For example:

    max(X,Y,Max) :- if_then_else(X>Y,Max=X,Max=Y).
    
    ?- max(4,7,A).
    A = 7
    ?- max(8,2,A).
    A = 8
    

提交回复
热议问题