Prolog if-statement [英] Prolog if-statement
问题描述
我正在尝试实现一个如下工作的谓词:
pred: -
%这样做总是
%if-statement
%只执行此操作,当if语句为true时
%这样做也是如此,如果if语句为true,则为独立假。
我需要一个程序的这个功能,它有可选的gui(XPCE)。您可以使用
使用gui
$来启动(true)% p $ p>
或
start(false)%without gui
因为我不想用相同的逻辑编写两个不同的谓词,但是一次使用gui和另一个时间没有,我想有一个谓词,只有在调用start(true)时调用gui-code。
感谢您的帮助!
解决方案标准Prologif-statement是:
(如果 - >那么;其他)
其中
如果
,然后
,Else
是目标。您可以轻松地使用谓词的定义来打开谓词的参数start / 1
:pred: -
%普通代码
(开始(真实) - >
%gui-only代码
;%non-gui代码
),
%普通代码
当没有
否则
目标,你可以用目标true
替换它。当If
目标失败时,目标(如果 - >然后)
将失败。即(如果 - >那么)
目标相当于(如果 - >那么;失败)
,而不是(如果 - >那么;是)
。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!
解决方案The standard Prolog "if-statement" is:
(If -> Then; Else)
where
If
,Then
, andElse
are goals. You can use if easily on the definition of your predicate to switch on the argument of the predicatestart/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 goaltrue
. The goal(If -> Then)
fails when theIf
goal fails. I.e. the(If -> Then)
goal is equivalent to(If -> Then; fail)
, not to(If -> Then; true)
.这篇关于Prolog if-statement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!