6.1.12:Using
Parentheses in Chunk Expressions
丸括弧をチャンク表現式で使う
You
use parentheses in chunk expressions for the same reasons
they're used in arithmetic:
•
To make a complex expression clearer.
• To change the order in which the parts of the
expression are evaluated.
算数で丸括弧が使われるのと同じ理由でチャンク表現式でも丸括弧を使います。
・複雑な表現式を明確にするため
・表現式内の計算順序を変更するため
For example, consider the following statement:
例として、次のステイトメントを考えてみてください:
put item
2 of word 3 of "a,b,c i,j,k x,y,z" -- BAD
文字列「a,b,c
i,j,k x,y,z」の3番目の単語の二番目のアイテム;これは悪い例です
The desired result is "y", the second item in the third
word. But the statement above causes an execution error,
because it asks for an item of a word, and words can't
contain items. You can obtain the desired result by using
parentheses to change the order of evaluation:
結果として得たいのは「y」、つまり3番目の単語の二番目のアイテムです。上の例のステイトメントではエラーが発生します。何故なら、このステイトメントで指定しているのは一つの単語内の一つのアイテムだからで、単語はアイテムを包含できないからだ。ところが、丸括弧を使うことで計算順序を変更し望み通りの結果を得ることができるのです。
put item
2 of (word 3 of "a,b,c i,j,k x,y,z") -- good
(文字列「a,b,c
i,j,k x,y,z」の3番目の単語)の二番目のアイテム;これはエラーになりません
【訳注】日本語に直すと非常に判り難いのです;文字通り算数で習った()の使い方を思い出してください
In the example above, Transcript gets the third word first,
then gets the second item in that word. By adding
parentheses around "word 3 of "a,b,c i,j,k x,y,z")", you
force Transcript to evaluate that part of the chunk
expression first. The value of the expression in
parentheses is "x,y,z", and item 2 of "x,y,z" is "y".
上の例では、トランスクリプトはまず三番目の単語を見つけ出し、次にその単語内の二番目のアイテムを探します。「
"word 3 of "a,b,c
i,j,k x,y,z")"」に丸括弧を付けることで、トランスクリプトが丸括弧内のチャンク表現式をまず計算させるようにさせます。丸括弧内の表現式の値は「"x,y,z"」となり、「"x,y,z"」の二番目のアイテムは「y」になります。
As with arithmetic expressions, the parts of a chunk
expression that are in parentheses are evaluated first. If
parentheses are nested, the chunk expression within the
innermost set of parentheses is evaluated first. The part
that is enclosed in parentheses must be a valid chunk
expression, as well as being part of a larger chunk
expression:
算数での計算式同様、丸括弧内のチャンク表現式の部分は最初に計算されます。丸括弧が入れ子になっていると、最も内側にある丸括弧内の部分が最初に計算されます。丸括弧に括られた部分は、一つの大きなチャンク表現式の一部分であると同時に、正しいチャンク表現でなければなりません。
put line
2 of word 1 to 15 of myValue -- won't
work : 誤り
put line 2 of word (1 to 15 of myValue) -- won't
work : 誤り
put line 2 of word 1 to 15 (of myValue) -- won't
work : 誤り
put line 2 of (word 1 to 15 of myValue) --
works! : 巧く行きます!
変数myValueの一番目から15番目までの単語の二行目
()で括られた部分に注目してください
The first of the above examples
doesn't work for much the same reason as the previous
example: words can't contain lines.The second and third
examples don't work because neither "1 to 15 of myValue"
nor "of myValue" is a valid chunk expression. However,
"word 1 to 15 of myValue" is a valid chunk expression, so
the last example works.
上で最初の例は、その前の例と同様の理由でエラーとなります:単語は行を包含できません。二番目と三番めの例では、「"1
to 15 of myValue"」も「"of
myValue"」も正しいチャンク表現式ではありません。他方、「"word
1 to 15 of myValue"」は正しいチャンク表現式なので、最後の例では正しい結果が得られます。