Prolog if-statement

2019-03-04 02: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条回答
神经病院院长
2楼-- · 2019-03-04 03:02

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

查看更多
登录 后发表回答