Prolog if 语句 [英] Prolog if-statement
问题描述
我正在尝试实现一个如下工作的谓词:
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.
我需要一个程序的这个功能,它可以选择是否有 gui (XPCE).你可以调用它
I need this functionality for a program, which has optionality a gui (XPCE) or not. You can call it with
start(true) % with gui
或
start(false) % without gui
因为我不想用相同的逻辑编写两个不同的谓词,但一次使用 gui,另一次没有,我想要一个谓词,仅当 start(true) 被调用时才调用 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.
感谢您的帮助!
推荐答案
标准的Prologif-statement"是:
The standard Prolog "if-statement" is:
(If -> Then; Else)
其中 If
、Then
和 Else
是目标.您可以轻松地在谓词的定义上使用 if 来打开谓词 start/1
的参数:
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
当没有Else
目标时,可以用目标true
代替.当 If
目标失败时,目标 (If -> Then)
失败.IE.(If -> Then)
目标等价于 (If -> Then; fail)
,而不是 (If -> Then; true)
.
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)
.
这篇关于Prolog if 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!