'if' in prolog?

前端 未结 10 1498
小鲜肉
小鲜肉 2020-12-03 02:12

Is there a way to do an if in prolog, e.g. if a variable is 0, then to do some actions (write text to the terminal). An else isn\'t even needed, but I can\'t find any docume

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 03:04

    The best thing to do is to use the so-called cuts, which has the symbol !.

    if_then_else(Condition, Action1, Action2) :- Condition, !, Action1.  
    if_then_else(Condition, Action1, Action2) :- Action2.
    

    The above is the basic structure of a condition function.

    To exemplify, here's the max function:

    max(X,Y,X):-X>Y,!.  
    max(X,Y,Y):-Y=

    I suggest reading more documentation on cuts, but in general they are like breakpoints. Ex.: In case the first max function returns a true value, the second function is not verified.

    PS: I'm fairly new to Prolog, but this is what I've found out.

提交回复
热议问题