循环语句的else子句的启发和影响?

发布于 2024-09-03 05:46:53 字数 432 浏览 5 评论 0 原文

Python 循环语句可能有一个 else 子句,当且仅当循环break 终止时才执行该子句。换句话说,当条件变为 False(使用 while)或迭代器耗尽时(使用 for)。

这个循环-else 结构是否源自另一种语言(无论是理论上的还是实际实现的)?它是否已被采用任何较新的语言?

也许我应该问前者,但他肯定太忙了,无暇进行这种徒劳的询问。 ;-)

相关讨论和示例: 在 for 循环中使用“else”的 Pythonic 方法

Python loop statements may have an else clause which is executed if and only if the loop is not terminated by a break. In other words, when the condition becomes False (with while) or when the iterator is exhausted (with for).

Does this loop-else construct originate from another language (either theoretical or actually implemented)? Has it been taken up in any newer language?

Maybe I should ask the former of Guido, but surely he is too busy for such a futile inquiry. ;-)

Related discussion and examples:
Pythonic ways to use ‘else’ in a for loop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诺曦 2024-09-10 05:46:53

Common Lisp 的 LOOP 宏中也有类似的功能,

...LOOP 提供了两个关键字,initial 和finally,它们引入了要在循环主体之外运行的代码。

在最初或最后之后,这些子句由所有 Lisp 形式组成,直到下一个循环子句开始或循环结束。所有初始形式都组合成一个序言,该序言在所有局部循环变量初始化之后和循环体之前立即运行一次。最后的形式类似地组合成尾声,在循环体的最后一次迭代之后运行。序言和结尾代码都可以引用局部循环变量。

即使循环体迭代零次,序言也始终运行。如果发生以下任一情况,循环可以返回而不运行尾声:

  • 执行 return 子句。
  • RETURN、RETURN-FROM 或其他控制构造的传输是从主体内的 Lisp 形式中调用的...

例如,在链接问题中找到的 Python 示例的一部分:

for v in known_variables:
    if self.bindings[v] is cell:
        return v
else:
    raise CannotSimplify

可能看起来像这样:

(loop for v in known-variables
  when (eq (gethash v (slot-value self bindings)) cell)
  do (return v)
  finally (signal cannot-simplify))

另一个观察:

Common Lisp 的条件系统也很独特。有一次,有人问它来自哪里,并被指出Kent Pitman 的论文,他说是从 Maclisp 那里得到的。同样,Common Lisp 看起来怪异的 FORMAT 函数显然来自 Multics,通过 丹·温雷布

共同点是,语言特征往往不会遵循最能激发该语言灵感的祖先语言,而是被喜爱它们的个人带到他们正在开发的任何新语言中。因此,如果您想找出 Python 的 for-else 的实际来源,我会查找添加它的人,并查看他们在此之前使用的语言。

A similar feature is found in Common Lisp's LOOP macro, described here by Peter Seibel:

...LOOP provides two keywords, initially and finally, that introduce code to be run outside the loop's main body.

After the initially or finally, these clauses consist of all the Lisp forms up to the start of the next loop clause or the end of the loop. All the initially forms are combined into a single prologue, which runs once, immediately after all the local loop variables are initialized and before the body of the loop. The finally forms are similarly combined into a epilogue to be run after the last iteration of the loop body. Both the prologue and epilogue code can refer to local loop variables.

The prologue is always run, even if the loop body iterates zero times. The loop can return without running the epilogue if any of the following happens:

  • A return clause executes.
  • RETURN , RETURN-FROM, or another transfer of control construct is called from within a Lisp form within the body...

For example, part of a Python sample found in the linked question:

for v in known_variables:
    if self.bindings[v] is cell:
        return v
else:
    raise CannotSimplify

might look something like this:

(loop for v in known-variables
  when (eq (gethash v (slot-value self bindings)) cell)
  do (return v)
  finally (signal cannot-simplify))

Another observation:

Common Lisp's condition system is also unique. Someone, once, asked where it came from and was pointed to Kent Pitman's paper, where he says got it from Maclisp. Similarly, Common Lisp's weird-looking FORMAT function apparently came from Multics via Dan Weinreb.

The common thread is that language features don't tend to follow from the ancestor language that most inspired this language, but are taken by individuals who loved them to whatever new language they're working on. So if you want to find out the actual source of Python's for-else, I'd look for who added it, and see what language they worked on prior to that.

时光匆匆的小流年 2024-09-10 05:46:53

我刚刚在这个更普遍的问题的评论中发现了一个很好的线索。用户 ΤΖΩΤΖIΟΥ 写道:

有人记得 FOR var ... NEXT var
… END FOR Sinclair QL 的 var
超级基本? NEXT 之间的一切
END FOR 将在最后执行
循环的,除非 EXIT FOR 是
发布。该语法更清晰:)

Sinclair QL 用户指南的 OCR 版本 恰好是漂浮在互联网上。上面写着:

NEXT 语句可以放在
环形。它导致控制转到
紧随其后的语句
开头关键字 FOR 或 REPeat。它
应被视为一种
与 EXIT 语句相反。由一个
奇怪的巧合,这两个词
NEXT 和 EXIT 都包含 EXT。思考
EXTension 到循环并且:

  • N 表示“现在重新开始”
  • 我的意思是“结束了”

一个有趣的例子如下:

警长有一把装了六枚子弹的枪
子弹,他要向
强盗,但还有两个条件适用:

  1. 如果他击中强盗,他就会停下来
    开火并返回道奇城。

  2. 如果他在击中目标之前子弹用完了
    强盗,他告诉他的伙伴观看
    强盗(警长)回来时
    前往道奇城。

100 REMark Western FOR with Epilogue
110 FOR bullets = 1 TO 6
120   PRINT "Take aim"
130   PRINT "FIRE A SHOT"
140   LET hit= RND(0 TO 1)
150   IF hit = 1 THEN EXIT bullets
160 NEXT bullets
170   PRINT "Watch Bandit"
180 END FOR bullets
190 PRINT "Return to Dodge City"

因此,在不同的(并且可以说不那么令人不安的)语法下,它的语义完全相同。

维基百科告诉我们,Sinclair QL 于 1984 年 2 月推出,作为 Sinclair ZX Spectrum 的后继产品,但未能取得商业成功。

I just came across a pretty good lead buried in the comments of this far more general question. User ΤΖΩΤΖΙΟΥ wrote:

Anyone remember the FOR var … NEXT var
… END FOR var of Sinclair QL's
SuperBasic? Everything between NEXT
and END FOR would execute at the end
of the loop, unless an EXIT FOR was
issued. That syntax was cleaner :)

An OCR rendition of Sinclair QL User Guide happens to float on the internet. It reads:

A NEXT statement may be placed in a
loop. It causes control to go to the
statement which is just after the
opening keyword FOR or REPeat. It
should be considered as a kind of
opposite to the EXIT statement. By a
curious coincidence, the two words
NEXT and EXIT both contain EXT. Think
of an EXTension to loops and:

  • N means "Now start again"
  • I means "It's ended"

A fascinating example follows:

The sheriff has a gun loaded with six
bullets and he is to fire at the
bandit but two more conditions apply:

  1. If he hits the bandit he stops
    firing and returns to Dodge City.

  2. If he runs out of bullets before he hits the
    bandit, he tells his partner to watch
    the bandit while he (sheriff) returns
    to Dodge City.

100 REMark Western FOR with Epilogue
110 FOR bullets = 1 TO 6
120   PRINT "Take aim"
130   PRINT "FIRE A SHOT"
140   LET hit= RND(0 TO 1)
150   IF hit = 1 THEN EXIT bullets
160 NEXT bullets
170   PRINT "Watch Bandit"
180 END FOR bullets
190 PRINT "Return to Dodge City"

So, under a different (and arguably less disturbing) syntax, it's exactly the same semantics.

Wikipedia tells us that the Sinclair QL launched in February 1984 as the successor to the Sinclair ZX Spectrum, but failed to achieve commercial success.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文