Prolog if-statement

久未见 提交于 2019-12-20 05:55:24

问题


I'm trying to implement a predicate that works as follows:

pred :-
   % do this always

   % if-statement
      %do this only, when if-statement is true

   % do this also always, independent if if-statement where true or false.

I need this functionality for a program, which has optionality a gui (XPCE) or not. You can call it with

start(true) % with gui

or

start(false) % without gui

Because I don't want to write two different predicates with the same logic, but one time with gui and another time without, I want to have one one predicate, that invokes the gui-code only if start(true) were invoked.

Thanks for your help!


回答1:


The standard Prolog "if-statement" is:

(If -> Then; Else)

where If, Then, and Else are goals. You can use if easily on the definition of your predicate to switch on the argument of the predicate start/1:

pred :-
    % common code
    (   start(true) ->
        % gui-only code
    ;   % non-gui code
    ),
    % common code

When there's no Else goal, you can replace it with the goal true. The goal (If -> Then) fails when the If goal fails. I.e. the (If -> Then) goal is equivalent to (If -> Then; fail), not to (If -> Then; true).



来源:https://stackoverflow.com/questions/20003675/prolog-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!