如何编写与 Set 一起使用的 Rebol 代码块(以编程方式)?
我想这样做:
>> 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发现括号不是
word!
。它们内置于 Rebol 中,解析器确保它们与您匹配——就像block!
一样。这是一件好事,否则系统周围都会有处理
()))()
不匹配的代码。你已经从那种痛苦中被拯救出来了!但如果您愿意,您可以用自己的方言重新发明这种痛苦,并使用 BEGIN 和 END 之类的单词,而不是利用永远有用的paren!
。 :P这是对您的代码的简约补丁:
请注意,您不能编写
copy ()
。一般来说,在do
方言中,括号的使用比块要复杂一些——它们具有双重优先权!解释器认为copy ()
意味着您正在尝试将要复制其结果的表达式括起来。 :(你可以通过将东西构建为块,然后仅在最后一刻转换它们来为自己省去一些麻烦:
PS 我不想通过指出括号实际上并不是必需的来分散你对问题的注意力:
但是好消息是,如果您愿意在这种情况下让括号服务于“更高的目的”,那么总是有
compose
组合就像使用“模板方言”,只会减少包含的表达式这是通过示例创建代码的好方法,但如果您生成的代码已经使用括号作为优先级,那么肯定会遇到麻烦!仍然...表明您的方言可以使用括号任何目的,就像它们可以赋予文字意义一样。
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 likeblock!
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-helpfulparen!
. :PHere's a minimalist patch to your code:
Note that you cannot write
copy ()
. Generally speaking, parentheses are a little trickier to work with in thedo
dialect than blocks—they are serving double-duty for precedence! The interpreter thinkscopy ()
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:
P.S. I didn't want to distract from your question by pointing out that the parentheses weren't actually necessary:
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
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.