primes.splをドキュメントを参照しながら解説

はじめに

主要な部分はhello.splの解説を読んでいただくとして、この項ではprimes.spl内で初めて出てくる用語について解説していきます。

primes.splに関係する項は、input、gotos、conditional statementsです。

Input

20、21行目に以下のような記述があります。

Juliet:
 Listen to your heart!

ここについて説明しているのがInputです。

The input statements work just like the output statements, except that they read instead of write. To read a number, as in this program, use the sentence ``Listen to your heart.'' To read a character, use ``Open your mind.'' The value will be assigned to the character being spoken to, as usual.

インプットステートメントは書く代わりに読むことを除いて、アウトプットステートメントのように働きます。このプログラムのように数字を読むには、``Listen to your heart.''という文を使用してください。文字を読むためには、``Open your mind.''を使います。値はいつものように、話される登場人物に割り当てられるでしょう。

まあそのままの意味ですが、Listen to your heart.で数字を入力。Open your mind.で、文字の入力が出来るということです。

Gotos

次は67、68行目と98〜100行目について。

67、68行目
Juliet:
 Let us return to scene II.
98〜100行目
Juliet:
 Thou art as handsome as the sum of thyself and my chihuahua!
 Let us return to scene I.

ここについてはGotosで説明されています。

A sentence like ``Let us return to scene III'' means simply ``goto scene III''. Instead of ``let us'', you may use ``we shall'' or ``we must'', and instead of ``return to'', you may use ``proceed to''. If you specify a scene, it refers to that scene in the current act. There is no way to refer to a specific scene in another act - you have to settle for jumping to the act itself.

``Let us return to scene III''のような文は、単に``goto scene III''を意味します。``let us''の代わりに``we shall''や``we must''を使用したり、``return to''の代わりに``proceed to''を使用することが出来ます。あなたがSceneを指定すれば、現在のActのそのSceneを参照します。別のActの特定のSceneを参照する方法はありません。あなたはActを飛び越えることを我慢しなければなりません。

Let us return to scene Xのように記述することによって、gotoが実現できます。gotoできるのは現在のActの特定のSceneに限定されるようです。

この項を読む限りでは、以下の6パターンの書き方が認められるようですね。

Conditional statements

次は35〜39行目、53〜57行目、59〜65行目について。

35〜39行目
Juliet:
 Art thou more cunning than the Ghost?

Romeo:
 If so, let us proceed to scene V.
53〜57行目
Juliet:
 Am I better than you?

Hamlet:
 If so, let us proceed to scene III.
59〜65行目
Juliet:
 Is the remainder of the quotient between Romeo and me as good as
 nothing?

Hamlet:
 If so, let us proceed to scene IV.
 Thou art as bold as the sum of thyself and a roman.

ここについてはConditional statementsで説明されています。

Conditional statements come in two easy steps, as illustrated by the following code fragment:

Juliet:
 Am I better than you?

Hamlet:
 If so, let us proceed to scene III.

First, someone voices a question. This is some sort of comparison, which will be either true or false. But more on that later.

Then comes, at some later point, a conditional statement. This is constructed by putting either ``if so'' (or ``if not'') and a comma in front of any sentence - that sentence is only executed if the answer to the last question was yes (or no).

This is pretty much like how you would do conditional jumps and things in many assembly languages.

条件文は以下のコード片に説明されるように簡単な2ステップで始まります。

Juliet:
 Am I better than you?

Hamlet:
 If so, let us proceed to scene III.

この時、条件文は何らかの後のポイントで来ます。これはどんな文の前にでも``if so'' (または ``if not'')とコンマを置くことによって組み立てられます。その文は最後の質問の答えがはい(または、いいえ)であった場合にだけ実行されます。

This is pretty much like how you would do conditional jumps and things in many assembly languages.(この段落は良くわかんない。「多くのアセンブリ言語のジャンプ構造と同じようなものだぜ」と書いてあるような気がする。)

If文ですね。if so, 条件またはif not, 条件という形で書くことが出来ます。ifがくる前の質問文がif部分で評価され、結果が真であった時に,の後に続く文が実行されます。

Comparisons

そしてComparisonsに続いています。

Comparisons are constructed the way you would expect: ``is $X$ as good as $Y$'' tests for equality, with $X$ and $Y$ being arbitrary values. You may substitute ``good'' with any adjective. ``is $X$ better than $Y$'' tests if $X > Y$. This works for any positive comparative. If you want to test whether $X < Y$, use a negative comparative, such as ``worse''.

If you want to invert the test, say ``not as good as'' or ``not better than''.

比較はあなたが以下を予想する方法で構成されます。``is X as good as Y''は任意の値であるXとYが一致しているかどうかを評価します。``is X better than Y''はif X > Yを評価します。これはどのようなpositive comparativeのためにも使用できます。もし、あなたがX < Yかどうかを比較したいのであればnegative comparativeである``worse''を使用してください。

または、評価を逆にしたいのであれば``not as good as''または``not better than''を使用してください。

上記項目で出てきた構文をCで置き換えると以下のようになります。

is X as good as Y
if (X == Y);
is X better than Y
if (X > Y);
is X worse than Y
if (X < Y);
is X not as good as Y
if (X != Y);
is X not better than Y
if ( !(X > Y) );
is X not worse than Y
if ( !(X < Y) );

if文が使えるようになると、もうなんでも出来てしまいますね。