プリンキピア

第7章高水準プログラミング言語

Recursion再帰
Recursion is the sequence in which a function calls itself. If a function calls itself, then the next instance of the function will also call itself. This proceeds onward infinitely until the call stack runs out of memory, unless there is a means within the function to shut down the process. This condition is known as stack overflow.
英語のヒント
再帰とは、関数が自分自身を呼び出す流れである。関数が自分自身を呼び出すと、次の関数の実行でも自分自身を呼び出す。関数内にこの処理を止める仕組みがなければ、呼び出しスタックのメモリが尽きるまで、これが際限なく続く。この状態をスタックオーバーフローという。
Certain problems or tasks that we want a program to engage with lend themselves to recursion, such as with a rocket launch. If we want to program the display of a countdown sequence to launch, we can do it recursively. The signature, known as a prototype in C++, might look like this:
英語のヒント
プログラムで扱いたい問題や作業の中には、ロケットの打ち上げのように、再帰に適したものがある。打ち上げまでのカウントダウン表示をプログラムしたいなら、再帰を使って実現できる。C++でプロトタイプと呼ばれるシグネチャーは、次のようになるだろう。
void countDown(long);
As indicated by the void, the function will return nothing to its calling point. It has a parameter that represents the starting point of the countdown. Therefore, if we want to start the countdown at 100, the call will look like this:
英語のヒント
voidが示すとおり、この関数は呼び出し元へ何も返さない。カウントダウンの開始点を表す引数を一つ持つ。そのため、100からカウントダウンを始めたいなら、呼び出しは次のようになる。
countDown(100);
Building out the recursive function could be done as follows:
英語のヒント
再帰関数の中身は、次のように書ける。
void countDown(long count) {
   if (count < 0)
   {
      return;
   }
   cout << count << endl;
   count = count - 1;
   countDown(count);
}

Well-Structured Programs適切に構造化されたプログラム

A major challenge with writing well-structured programs is the complexity of today’s systems, especially when they are influenced by so many programmers. A common dilemma is how to divide and conquer the task of writing programs when the effort relies on the notion of modularization, or splitting the large job into independent units which may be then called upon. We can now expand that concept to include entities that exist in completely separate pieces of source code.
英語のヒント
適切に構造化されたプログラムを書くうえで大きな課題となるのが、現在のシステムの複雑さであり、とりわけ多くのプログラマーが関わる場合にはそうである。よくある難問は、モジュール化、つまり大きな仕事を、必要に応じて呼び出せる独立した単位に分割する考え方を土台として、プログラムを書く作業をどう分割して片付けるかである。ここで、この考え方を、完全に別々のソースコードに存在する構成要素まで含むように広げられる。
Programs are very efficiently built out of modules, as shown in Figure 7.18. A module is a component of a program and has a public interface. For example, in Java the interface only includes documentation of what a user needs to access an object derived from a class, along with its attributes and methods. The interface documents exactly how components can be used by other programmers: what they are named, what, if anything, they require to do their jobs, and what they may or may not return. Documentation is necessary for another programmer to know how to employ them.
英語のヒント
図7.18に示すように、モジュールを使うとプログラムを非常に効率よく構築できる。モジュールは、公開インターフェースを持つプログラムの構成要素である。たとえばJavaのインターフェースには、クラスから作ったオブジェクトと、その属性やメソッドにアクセスするために、利用者が知る必要のあることだけが文書化される。インターフェースには、ほかのプログラマーが構成要素をどう利用できるかが、正確に記される。すなわち、どんな名前が付いているか、仕事をするために何か必要ならそれは何か、そして何を返せるのか、あるいは返せないのかである。別のプログラマーが使い方を知るには、この文書が必要である。
Figure 7.18

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 7.18 This diagram shows a program made up of multiple modules, each of which contains multiple functions. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.18 この図は、複数のモジュールからなるプログラムを示している。各モジュールには、複数の関数が含まれる。(出典:著作権 Rice University、OpenStax、CC BY 4.0ライセンス)
This allows us to engage in information hiding: the hiding of bits and bytes of implementation that the user does not need to know to make use of the module. A good example is the PHP print function; its implementation is not located in any place that we can see. Rather it comes from a module that somebody has programmed and is shipped with the language API, such as in Figure 7.19.
英語のヒント
これによって、情報隠蔽を行える。これは、モジュールを利用するために利用者が知る必要のない、実装の細部を隠すことである。よい例がPHPのprint関数で、その実装は、こちらから見える場所には置かれていない。図7.19のように、誰かが作成し、その言語のAPIと一緒に配布するモジュールから提供される。
Figure 7.19

credit: The PHP Documentation Group, CC BY 3.0 · CC BY 3.0 · Source出典

Figure 7.19 This portion of the PHP manual gives the documentation for the PHP print construct. (credit: The PHP Documentation Group, CC BY 3.0)
図7.19 PHPマニュアルのこの箇所には、PHPのprint構文の説明が載っている。(クレジット:The PHP Documentation Group、CC BY 3.0)

Exception Handling例外処理

No code is perfect; therefore, any computer program can generate runtime errors. This type of error usually indicates that an exceptional condition has occurred. These errors are generated by a hardware-detected run-time error or an unusual condition detected by software. Some of these errors include arithmetic overflow, division by zero, end-of-file on input, and user-defined conditions (not necessarily hardware or software errors).
英語のヒント
完璧なコードはないので、どのコンピュータープログラムでも実行時エラーが発生しうる。この種のエラーは、通常、例外的な状態が生じたことを示す。これらのエラーは、ハードウェアが検出した実行時エラーや、ソフトウェアが検出した通常とは異なる状態によって発生する。例として、算術オーバーフロー、ゼロによる除算、入力時のファイル終端、利用者が定義した条件がある。最後のものは、必ずしもハードウェアやソフトウェアのエラーではない。
Most modern languages divide errors by category: a runtime error is an exception that is serious enough that it cannot or should not be handled by the software, and an exception is an unusual behavior that can be recovered using exception handling support the programming language provides (e.g., try/catch clause in Java). The proper terminology is that an error or exception has been thrown.
英語のヒント
現代の言語の多くは、エラーを種類によって分ける。実行時エラーは、ソフトウェアで処理できない、または処理すべきでないほど重大な例外である。一方、例外は、そのプログラミング言語が提供する例外処理機能、たとえばJavaのtry/catch節を使って回復できる、通常とは異なる振る舞いである。用語としては、エラーまたは例外が「投げられた」と表現する。

Files and Input/Outputファイルと入出力

File handling features and the input/output (I/O) capabilities (facilities that allow a program to communicate with the outside world) are highly dependent on both the operating system which is hosting the program and the HLL. All HLLs differ in the way that these are handled. The following are some examples of different output statements in various HLLs:
英語のヒント
ファイル処理機能と入出力(I/O)機能は、プログラムを動かすオペレーティングシステムと、高水準言語の両方に大きく依存する。入出力機能とは、プログラムが外の世界とやり取りできるようにする機能である。これらの扱い方は、高水準言語ごとに異なる。以下に、さまざまな高水準言語の出力文の例を挙げる。
• Java: System.out.println("Hello");
• Java: System.out.println("Hello");
• C#: Console.Writeln("Hello");
• C#: Console.Writeln("Hello");
• C: printf("Hello");
• C: printf("Hello");
• C++: cout << "Hello";
• C++: cout << "Hello";
• JavaScript: alert("Hello");
• JavaScript: alert("Hello");
• PHP: print("Hello");
• PHP: print("Hello");
There are as many ways of handling file I/O as there are HLLs. This is a subject that requires research with every new language.
英語のヒント
ファイル入出力の扱い方は、高水準言語の数だけある。新しい言語を学ぶたびに調べる必要のある分野である。