如何编写与 Set 一起使用的 Rebol 代码块(以编程方式)?

发布于 2024-08-15 00:37:54 字数 703 浏览 3 评论 0原文

我想这样做:

>> SET [a b] reduce [(ask "a: ") (ask "b: ")]
a: 1
b: 2
== ["1" "2"]
>>

以编程方式:

args: [a b]
block: copy []
foreach arg args [
  append block to-word "("
  append block 'ask
  append block rejoin [arg ": "]
  append block to-word ")"
]
set args reduce block

但我收到此错误:

>>     foreach arg args [
[          append block to-word "("
[          append block 'ask
[          append block rejoin [arg ": "]
[          append block to-word ")"
[        ]
== [( ask "a: " ) ( ask "b: " )]
>>    set args reduce block
** Script Error: ( has no value
** Near: ( ask "a: " ) (
>>

I want to do this:

>> SET [a b] reduce [(ask "a: ") (ask "b: ")]
a: 1
b: 2
== ["1" "2"]
>>

Programmatically:

args: [a b]
block: copy []
foreach arg args [
  append block to-word "("
  append block 'ask
  append block rejoin [arg ": "]
  append block to-word ")"
]
set args reduce block

But I get this error:

>>     foreach arg args [
[          append block to-word "("
[          append block 'ask
[          append block rejoin [arg ": "]
[          append block to-word ")"
[        ]
== [( ask "a: " ) ( ask "b: " )]
>>    set args reduce block
** Script Error: ( has no value
** Near: ( ask "a: " ) (
>>

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

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

发布评论

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

评论(1

杀手六號 2024-08-22 00:37:54

您发现括号不是 word!。它们内置于 Rebol 中,解析器确保它们与您匹配——就像 block! 一样。

这是一件好事,否则系统周围都会有处理 ()))() 不匹配的代码。你已经从那种痛苦中被拯救出来了!但如果您愿意,您可以用自己的方言重新发明这种痛苦,并使用 BEGIN 和 END 之类的单词,而不是利用永远有用的 paren!。 :P

这是对您的代码的简约补丁:

args: [a b]
block: copy []
foreach arg args [
   p: to-paren []
   append/only block p
   append p 'ask
   append p rejoin [arg ": "]
]
set args reduce block

请注意,您不能编写 copy ()。一般来说,在 do 方言中,括号的使用比块要复杂一些——它们具有双重优先权!解释器认为 copy () 意味着您正在尝试将要复制其结果的表达式括起来。 :(

你可以通过将东西构建为块,然后仅在最后一刻转换它们来为自己省去一些麻烦:

>> to-paren [ask "a: "]
== (ask "a: ")

PS 我不想通过指出括号实际上并不是必需的来分散你对问题的注意力:

>> SET [a b] reduce [ask "a: " ask "b: "]

但是好消息是,如果您愿意在这种情况下让括号服务于“更高的目的”,那么总是有 compose

args: [a b]
block: copy []
foreach arg args [
    append block compose [ask (rejoin [arg ": "])]
]
set args reduce block

组合就像使用“模板方言”,只会减少包含的表达式这是通过示例创建代码的好方法,但如果您生成的代码已经使用括号作为优先级,那么肯定会遇到麻烦!仍然...表明您的方言可以使用括号任何目的,就像它们可以赋予文字意义一样。

What you've discovered is that parentheses are not word!s. They are built-in to Rebol, and the parser makes sure they match up for you—just like block! does.

This is a good thing—otherwise all around the system would be code handling the ()))() mismatches. You've been saved from that pain! But if you want then you can reinvent this pain in your own dialects, and use words like BEGIN and END instead of leveraging the ever-helpful paren!. :P

Here's a minimalist patch to your code:

args: [a b]
block: copy []
foreach arg args [
   p: to-paren []
   append/only block p
   append p 'ask
   append p rejoin [arg ": "]
]
set args reduce block

Note that you cannot write copy (). Generally speaking, parentheses are a little trickier to work with in the do dialect than blocks—they are serving double-duty for precedence! The interpreter thinks copy () means you're trying to parenthesize an expression whose results you want to copy. :(

You can save yourself some headaches by building things up as blocks, and then converting them only at the last minute:

>> to-paren [ask "a: "]
== (ask "a: ")

P.S. I didn't want to distract from your question by pointing out that the parentheses weren't actually necessary:

>> SET [a b] reduce [ask "a: " ask "b: "]

But the good news about that is that if you're willing to let parens serve a "higher purpose" in this case, there's always compose

args: [a b]
block: copy []
foreach arg args [
    append block compose [ask (rejoin [arg ": "])]
]
set args reduce block

Composing is like using a "templating dialect", which only reduces the expressions contained in parens, leaving everything else as-is. It's a good way to create code by example, but definitely does run into trouble if your generated code uses parentheses already for precedence! Still...goes to show that your dialects can use parentheses for any purpose, just like they can give meaning to words.

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