C语言风格的REBOL中的循环 [英] C-style for loops in REBOL

查看:125
本文介绍了C语言风格的REBOL中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在REBOL中编写C样式的for循环:

I attempted to write a C-style for-loop in REBOL:

for [i: 0] [i < 10] [i: i + 1] [
    print i
]

此语法似乎不正确,

*** ERROR
** Script error: for does not allow block! for its 'word argument
** Where: try do either either either -apply-
** Near: try load/all join %/users/try-REBOL/data/ system/script/args...

REBOL是否具有类似于C样式的for循环的内置函数,还是我需要自己实现此功能?

Does REBOL have any built-in function that is similar to a C-style for loop, or will I need to implement this function myself?

类似C的语言中的等效构造看起来像这样,但是我不确定是否可以在REBOL中实现相同的模式:

The equivalent construct in a C-like language would look like this, but I'm not sure if it's possible to implement the same pattern in REBOL:

for(i = 0; i < 10; i++){
    print(i);
}

推荐答案

由于标签,我假设这个问题与Rebol 3有关.

Because of the rebol3 tag, I'll assume this question pertains to Rebol 3.

为Rebol 3提议的"CFOR"

对于Rebol 3,有一个提案(得到了很多支持)非常类似于C样式for的通用循环",因此目前的名称是cfor以及:有关所有血腥细节,请参见 CureCode问题#884 .

For Rebol 3, there is a proposal (which got quite a bit of support) for a "general loop" very much along the lines of a C-style for and therefore currently going under the name of cfor as well: see CureCode issue #884 for all the gory details.

这包括Ladislav的原始实现的改进版本,为方便参考,我将在此重现当前版本(截至2014-05-17)(不讨论实现方面的大量内联注释)

This includes a much refined version of Ladislav's original implementation, the current (as of 2014-05-17) version I'll reproduce here (without the extensive inline comments discussing implementation aspects) for the sake of easy reference:

cfor: func [  ; Not this name
    "General loop based on an initial state, test, and per-loop change."
    init [block! object!] "Words & initial values as object spec (local)"
    test [block!] "Continue if condition is true"
    bump [block!] "Move to the next step in the loop"
    body [block!] "Block to evaluate each time"
    /local ret
] [
    if block? init [init: make object! init]

    test: bind/copy test init
    body: bind/copy body init
    bump: bind/copy bump init

    while test [set/any 'ret do body do bump get/any 'ret]
]


Rebol 3中用户级控件结构实现的一般问题

对于Rebol 3中所有用户级别的控制结构实现的一个重要的一般说明:R3中还没有类似Rebol 2的[throw]属性的信息(请参见

One important general remark for all user-level implementation of control constructs in Rebol 3: there is no analogue to Rebol 2's [throw] attribute in R3 yet (see CureCode issue #539), so such user-written ("mezzanine", in Rebol lingo) control or loop functions have problems, in general.

尤其是,此CFOR将错误地捕获returnexit.为了说明这一点,请考虑以下功能:

In particular, this CFOR would incorrectly capture return and exit. To illustrate, consider the following function:

foo: function [] [
    print "before"
    cfor [i: 1] [i < 10] [++ i] [
        print i
        if i > 2 [return true]
    ]
    print "after"
    return false
]

您(正确)希望return实际上是从foo返回的.但是,如果您尝试上述方法,则会发现此期望值令人失望:

You'd (rightly) expect the return to actually return from foo. However, if you try the above, you'll find this expectation disappointed:

>> foo
before
1
2
3
after
== false

此注释当然适用于 all 在该线程中作为答案提供的用户级实现,直到错误#539被修复为止.

This remark of course applies to all the user-level implementation given as answers in this thread, until bug #539 is fixed.

这篇关于C语言风格的REBOL中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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