Prolog循环和条件语句? [英] Prolog loops and conditional statements?

查看:29
本文介绍了Prolog循环和条件语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Prolog 中有什么东西可以像 for 循环和 if then 条件一样工作吗?

Is there anything in Prolog that works like a for loop and if then condition?

推荐答案

if/then/else 可以通过 (->)/2 和 (;)/2 获得:

if/then/else can be obtained with (->)/2 and (;)/2:

(    If ->
     Then
;    Else
)

有时这很有用.但总的来说(当条件包含变量时),它会使您的程序不健全和不完整.只要可以使用模式匹配来描述条件,就应该使用模式匹配.然后,您不仅可以检查,还可以生成解决方案.

Sometimes this is useful. In general though (when the condition contains variables), it will make your programs unsound and incomplete. Whenever it is possible to describe the conditions with pattern matching, you should use pattern matching instead. You can then not only check but also generate solutions.

这篇关于Prolog循环和条件语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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