3
どのような状況では、ループ構造がインタープリタによるレッドの評価を必要としますか?どちらのループ構造でないのですか?レッドループ構造はいつ解釈する必要があります
アップデート(2015年4月20日):以下
私はいくつかの学生に言語をデモするために書いた小さなプログラムです。 2つの関数 'listと' findにforeachループがあります。しかし、foreachループはdoブロックにラップされていなければ実行されません。 foreachループの本体が間接的に指定されていないため、このシナリオは特に元の質問を促したものです。
Model.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
]