Prolog if-statement [英] Prolog if-statement

查看:101
本文介绍了Prolog if-statement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个如下工作的谓词:

  pred: -  
%这样做总是

%if-statement
%只执行此操作,当if语句为true时

%这样做也是如此,如果if语句为true,则为独立假。

我需要一个程序的这个功能,它有可选的gui(XPCE)。您可以使用

 使用gui 


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

这篇关于Prolog if-statement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆