6

I would think that the following Rebol 3 code:

x: [newline 1 2]
y: [
1 2]

print x
print new-line? x
print y
print new-line? y

should output:

<empty line>
1 2
true
<empty line>
1 2
true

but what is output is:

<empty line>
1 2
false
1 2
true

Both blocks, when reduced, should yield a newline character followed by '1' and '2' and so, IMO, should print identically. Less clear is whether new-line? on the two blocks should also give the same result since the newline keyword should be equivalent to the literal newline for this kind of test.

4

2 回答 2

3

new-line?检查并设置的标志new-line仅由 LOAD 和 MOLD 使用。对于程序中的所有其他语义目的,它可能不存在。

因此你xy完全不同。注意:

x: [newline 1 2]
y: [
1 2]

3 = length? x
2 = length? y

Rebol 的一个怪癖是它会挑选出这一位空白信息以存放在一个隐藏的地方。但可以说,换行的选择代表了在源代码中通常很重要的东西,如果你将它反射回文本中,你希望保留比其余空白更多的东西。

于 2013-01-23T17:32:14.373 回答
3

让我们从 NEWLINE 开始:newline是一个绑定到char!值的单词:

>> ? newline
NEWLINE is a char of value: #"^/"

这是 Rebol 的 Unicode 代码点 U+000A 的转义序列,通常用作换行(“LF”)控制代码。

因此,您的第一个示例代码[newline 1 2]与 NEW-LINE 函数无关。它简单地描述了一个包含三个值的块:newline(a word!)、2(an integer!) 和3(another integer!)。如果您减少第一个示例中的块,您将获得另一个包含三个值的块:char!integer!integer!

>> reduce [newline 1 2]
== [#"^/" 1 2]

现在 PRINT 不仅 REDUCE,它还 REFORM(首先 REDUCE,然后 FORM)一个块参数。块的 FORM 将元素转换为字符串表示形式,然后将它们之间的空格连接起来:

>> form [1 2 3]
== "1 2 3"

将这些部分放在一起,我们终于知道如何获得您在第一个示例中看到的输出:

>> basis: [newline 1 2 3]
== [newline 1 2 3]
>> step1: reduce basis
== [#"^/" 1 2 3]
>> step2: form step1
== "^/1 2 3"
>> print step2

1 2 3

所以问题仍然存在,为什么第二个例子打印的不一样?

这是因为 FORM(由 PRINT 使用,如上所述)在从块转换为字符串时不遵守 NEW-LINE 标志。

该标志是“元数据”,与块内元素的索引位置没有什么不同。所以就像你没有元素在索引位置 8 和 6 只是因为你写了一个[8 6]像在某些系统上代表换行符:[1 newline 2].

这最终将我们带到了难题的最后一部分:NEW-LINE?不检查给定字符串是否表示换行符。它检查一个块(在其当前位置)是否设置了换行标志。

于 2013-01-23T17:43:51.713 回答