'if' in prolog?

前端 未结 10 1488
小鲜肉
小鲜肉 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 02:59

    Prolog predicates 'unify' -

    So, in an imperative langauge I'd write

    function bazoo(integer foo)
    {
       if(foo == 5)
           doSomething();
       else
           doSomeOtherThing();
    }
    

    In Prolog I'd write

    bazoo(5) :-  doSomething.
    bazoo(Foo) :- Foo =/= 5, doSomeOtherThing.
    

    which, when you understand both styles, is actually a lot clearer.
    "I'm bazoo for the special case when foo is 5"
    "I'm bazoo for the normal case when foo isn't 5"

提交回复
热议问题