gpt4 book ai didi

red - 什么时候红色循环结构需要解释

转载 作者:行者123 更新时间:2023-12-02 01:26:53 25 4
gpt4 key购买 nike

在什么情况下循环结构需要由 Red 解释器进行评估?哪些循环结构没有?

更新 (4/20/2015):

下面是我写的一个小程序,用于向一些学生演示该语言。其中两个函数 'list 和 'find 有 foreach 循环。但是 foreach 循环不会运行,除非它们被包装在一个 do 块中。这种情况尤其是促使最初问题的原因,因为没有间接指定 foreach 循环的主体。

型号.red

Red [
Title: "Model"
Author: "Thomas Royko"
Version: 0.0.1
]

entry!: make object! [
name: ""
quantity: 0
notes: ""
display: does [
print ["-- " name]
print ["-- " quantity]
print ["-- " notes newline]
]
]

enter: func [
entries value
/local quantity notes
] [
qt: load ask "Enter quantity: "
nt: ask "Enter notes: "
append entries make entry! copy [
name: value
quantity: qt
notes: nt
]
print ""
]

find: func [
entries value
/local found entry
] [
found: false
do [
foreach entry entries [
if entry/name = value [
entry/display
found: true
break
]
]
]
if not found [
print ["** No entry with code" value newline]
]
]

list: func [
entries
] [
do [
foreach entry entries [
entry/display
]
]
]

finish: func [
entries
] [
save %inventory.db entries
quit
]

Console.red
Red [
Title: "Console"
Author: "Thomas Royko"
Version: 0.0.1
]

#include %Bindings.red
#include %Model.red

entries: either exists? %inventory.db [
reduce load %inventory.db
] [
copy []
]

rule: [
(value: "")
1 [
["e" space copy value to end (enter entries value)] |
["f" space copy value to end (find entries value)] |
["l" to end (list entries)] |
["q" to end (finish entries)] |
[(print ["** Code not recognized" newline])]
]
]

forever [
parse ask "Command: " rule
]

最佳答案

它不依赖于循环构造,而是取决于您指定循环体块和(当它适用时)条件块的方式。如果这些块不是按字面指定的,而是间接使用单词指定的,那么它将被解释,因为编译器无法静态确定它们。例如:

n: 3 until [print n n = 0]

是可编译的,而:
body: [print n n = 0]
n: 3 until body

不是。

关于red - 什么时候红色循环结构需要解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com