编程中的削减那么糟糕吗? [英] Are cuts that bad in programming?

查看:57
本文介绍了编程中的削减那么糟糕吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本学期我将参加一门 AI 课程,我们正在学习 Prolog.我们的讲师告诉我们尽量避免在我们的作业中使用删减,但是,对于一些问题,我似乎无法避免使用它们.我只是好奇为什么削减被认为是一种罪过(讲师的话)?我知道这是一种捷径,但我已经使用它们,确切地知道它们如何影响我的程序.

I am taking an AI course this semester in which we are learning Prolog. Our lecturer has told us to try and avoid using cuts in our assignment, however, for a couple of the questions I can't seem to avoid using them. I'm just curious why are cuts considered a sin (lecturers words)? I understand it's kind of a short cut but I have used them knowing exactly how they effect my program.

推荐答案

我同意 @dasblinkenlight 和 @mbratch.此外,我认为从绿色切割和红色切割的角度来思考是有帮助的.

I agree with @dasblinkenlight and @mbratch. Additionally, I think it's helpful to think in terms of green cuts and red cuts.

绿色切割不影响程序的逻辑行为,只影响性能.它们是您告诉 Prolog 的一种方式,您知道如果它继续运行,它将不会产生任何结果.绿色削减从来都不是必要——它们只是提高了性能.当您第一次学习 Prolog 时,还有很多其他事情需要处理,这似乎只是为了一点点好处而增加了额外的复杂性.

A green cut is one that doesn't affect the logical behavior of the program, just the performance. They're a way for you to tell Prolog that you know if it keeps going it's not going to bear any fruit. Green cuts are never necessary—they just improve performance. There's so much else to get a handle on when you're first learning Prolog, it just seems like adding extra complexity for a small benefit.

红色切割确实会影响程序的行为.正如@mbratch 所说,新用户经常削减开支以整理"输出.新用户通常将 Prolog 查询提示视为他们程序的用户界面.这些削减使得它们的谓词在使输出更好的过程中变得不那么通用和不太有用.还有一些更清晰的替代方法,比如 once/1 ,它会给你一个单一的结果.专家非常巧妙地使用红色切割——在某些情况下,它比逻辑方法更有效——但如果你可以使用纯逻辑公式,那就更可取了.通常,在使用 cut 时出错的谓词会出现向后正确性"的问题,这些问题稍后会在您依赖该谓词作为其他谓词的一部分时出现.这些可能难以调试和修复.

A red cut does affect the behavior of the program. As @mbratch said, new users often throw cuts around to "tidy up" the output. New users often treat the Prolog query prompt as the user interface for their program. These cuts make their predicates less general and less useful in the process of making the output nicer. There are also some alternatives that are clearer, like once/1 which gives you a single result. Experts use red cuts very delicately—there are situations where it is much more efficient than a logical approach—but if you have access to a purely logical formulation it's preferable. Often predicates with errors in the use of cut have problems with "backwards correctness" that arise later on when you're depending on the predicate as part of other predicates. These can be difficult to debug and fix.

我不确定我是否会称它们为罪",但对于初学者,我基本同意您的教授的看法.如果您在不使用剪切的情况下逻辑地解决问题方面有经验,则最好.然后,当您对什么是容易的,什么是困难的有了更好的认识时,就可以在以后介绍剪辑.过早使用它会让你依赖它作为过程编程的拐杖.

I'm not sure I'd call them a "sin" but I mostly agree with your professor for beginners. It's best if you get experienced at solving problems logically without using the cut. Then the cut can be introduced later when you have a better sense of what is easy and what is hard. Using it too much early on makes you depend on it as a crutch for procedural programming.

这篇关于编程中的削减那么糟糕吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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