有用的替代控制结构? [英] Useful alternative control structures?

查看:91
本文介绍了有用的替代控制结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当我编程时,我发现某些特定的控制结构对我非常有用,但在我的编程语言中不直接可用.我认为我最常见的愿望是一会儿分裂"(我不知道该怎么称呼):

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my most common desire is something like a "split while" (I have no idea what to actually call this):

{
    foo();
} split_while( condition ) {
    bar();
}

此代码的语义是始终运行foo(),然后检查条件.如果为true,则运行bar(),然后我们回到第一个块(因此再次运行foo(),依此类推).感谢 reddit用户zxqdms的评论,我了解到Donald E. Knuth在他的论文使用go to语句" (请参阅第279页).

The semantics of this code would be that foo() is always run, and then the condition is checked. If true, then bar() is run and we go back to the first block (thus running foo() again, etc). Thanks to a comment by reddit user zxqdms, I have learned that Donald E. Knuth writes about this structure in his paper "Structured programming with go to statements" (see page 279).

您认为哪些替代控制结构是组织计算的有用方法?

我在这里的目标是为自己和其他人提供有关代码结构的新思路,以改善分块和推理.

My goal here is to give myself and others new ways of thinking about structuring code, in order to improve chunking and reasoning.

注意:我不是在问如何概括所有可能的控制结构,无论是使用jneif/goto,Lisp宏,延续,单子,组合器,夸克或其他任何东西.我问什么专业化对描述代码有用.

Note: I'm not asking about how to generalize all possible control structures, whether by using jne, if/goto, Lisp macros, continuations, monads, combinators, quarks, or whatever else. I'm asking what specializations are useful in describing code.

推荐答案

有时,我需要有一个带索引的foreach循环.可以这样写:

Sometimes, I need to have a foreach loop with an index. It could be written like this:

foreach (index i) (var item in list) {
  // ...
}

(我并不特别喜欢这种语法,但是您明白了)

(I'm not particularly fond of this syntax, but you get the idea)

这篇关于有用的替代控制结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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