第4章アルゴリズムの言語による実現:低水準プログラミング言語
The Imperative Programming Paradigm命令型プログラミングのパラダイム
In imperative programming, the programmer writes a series of steps that must be followed in order. Source code spells out a precise series of operations that the computer must execute in order. Since the computer is told to take specific actions and execute these statements, the language is referred to as “imperative.” An imperative is an order or command. Low-level languages are imperative languages, and middle-level languages, such as C, are imperative and include another paradigm. While low-level languages can mimic the style of a structured language, these properties are not inherent in the language itself and must be imposed by the programmer as a practice. Assembly code can easily be written in a non-structured way.
英語のヒント
命令型プログラミングでは、プログラマーは順に従うべき一連の手順を書く。ソースコードは、コンピューターが順番に実行する操作を、正確な列として明記する。特定の動作を行い、それらの文を実行するよう指示するため、「命令型」と呼ばれる。imperativeは命令や指令を意味する。低水準言語は命令型言語である。Cなどの中水準言語も命令型であり、別のパラダイムも含む。低水準言語で構造化言語の書き方をまねることはできるが、その性質が言語そのものに備わるわけではなく、プログラマーが実践として課す必要がある。アセンブリコードは、非構造化の形で容易に書けてしまう。
Declarative and Functional Programming宣言型・関数型プログラミング
Another type of programming, declarative programming, is a paradigm in which code dictates a desired outcome without specifying how that outcome is achieved. Declarative languages are an alternative to imperative languages. In a declarative language, the programmer declares the desired outcome, and it is the compiler’s job to create a series of imperative steps that obtains that outcome. For example, the Structured Query Language (SQL) used to query database systems makes it possible to specify what data should be retrieved from a database, without specifying how the database system should retrieve that data.
英語のヒント
別の種類である宣言型プログラミングは、結果をどう得るかを指定せずに、望む結果をコードで指示するパラダイムである。宣言型言語は、命令型言語に代わる選択肢である。プログラマーは望む結果を宣言し、その結果を得る命令手順の列を作るのはコンパイラーの仕事となる。たとえば、データベースに問い合わせる構造化問合せ言語(SQL)では、システムがどう取り出すかを指定せずに、どのデータを取得すべきかを指定できる。
Functional languages are another alternative to imperative languages. Recall that a function is a mathematical object that defines how to convert an input into an output. For example, given x = 4, the function converts the input 4 into the output 7. Functions can be defined in most programming languages and correspond to small sections of code that perform a specific task such as a calculation. Functions can be defined in most programming languages.
英語のヒント
関数型言語も、命令型言語に代わる選択肢である。関数は、入力を出力へ変換する方法を定める数学的な対象だということを思い出そう。たとえばx = 4が与えられると、関数は入力4を出力7へ変換する。関数は大半のプログラミング言語で定義でき、計算などの特定の仕事を行う短いコード部分に対応する。関数は大半のプログラミング言語で定義できる。
Functional programming is a programming paradigm in which algorithms are written as mathematical functions (Figure 4.10). In functional programming, practically every part of the program is written as a function. The programmer writes functions that convert inputs to outputs, and it is the compiler’s job to create imperative steps to evaluate the functions.
英語のヒント
関数型プログラミングは、アルゴリズムを数学的な関数として書くパラダイムである(図4.10)。事実上、プログラムのあらゆる部分を関数として書く。プログラマーは入力を出力に変換する関数を書き、それを評価する命令手順を作るのはコンパイラーの仕事となる。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 4.10 This diagram shows a functional programming example using Python. Here the function “LAMBDA” is used to increment the value of x by 1 whenever it is called. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.10 Pythonによる関数型プログラミングの例を示す。ここでは「LAMBDA」関数を使い、呼び出すたびにxの値を1増やす。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Declarative and functional languages are considered high-level languages because the compiler is creating these steps on behalf of the programmer. Functional languages are discussed in more detail in Chapter 7 High- Level Programming Languages.
英語のヒント
宣言型言語と関数型言語は、コンパイラーがプログラマーに代わって手順を作るため、高水準言語とみなされる。関数型言語は、第7章「高水準プログラミング言語」でさらに詳しく扱う。
Structured Programming構造化プログラミング
In low-level languages and early high-level languages such as BASIC, some special statements called conditional statements (using “if/then”) and iteration (called “loops”) are programmed using an operation called GOTO, a non-structured operation that instructs a computer to jump to an entirely different part of the program. In large programs, these jumps from one spot to another interact in complex ways, so the flow of execution is difficult to understand when attempting to read the code. These sorts of programs are criticized for being messy “spaghetti code” (Figure 4.11).
英語のヒント
低水準言語や、BASICのような初期の高水準言語では、「if/then」による条件文や、「ループ」と呼ぶ反復を、GOTOという操作でプログラムする。これは、プログラムのまったく別の部分へ飛ぶように指示する非構造化の操作である。大きなプログラムでは、ある場所から別の場所へのジャンプが複雑に絡み合い、コードを読んでも実行の流れを理解しにくい。この種のプログラムは、乱雑な「スパゲティコード」として批判される(図4.11)。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 4.11 The same program that prints the square root of an integer (using SQRT(z)) is shown in a structured format and an unstructured format. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.11 整数の平方根をSQRT(z)で求めて表示する同じプログラムを、構造化形式と非構造化形式で示す。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Newer languages were developed to help avoid spaghetti code. In a structured programming language, control flow leverages conditionals (e.g., “if-then”) and iteration statements (e.g., “while” or “do while”) and never uses GOTO statements. For example, C is a structured language that includes the conditional statements “if” and “switch,” and the iteration statements “for,” “while,” and “do.” In proper C, all the code sections that involve conditionals and iteration are written with these statements, and GOTO should not be used. Note that the fact that GOTO is provided as a keyword in the C language relates to the fact that C is a low-level language and programmers at that level are given the choice of using unstructured programming if necessary, although it is not recommended.
英語のヒント
スパゲティコードを避けやすくするため、より新しい言語が開発された。構造化プログラミング言語では、制御の流れに条件文(if-thenなど)と反復文(while、do whileなど)を使い、GOTO文は使わない。たとえばCは、条件文のifとswitch、反復文のfor、while、doを備える構造化言語である。適切なCでは、条件分岐や反復に関わるコードはすべてこれらの文で書き、GOTOは使うべきでない。CにGOTOというキーワードがあるのは、Cが低水準言語で、その水準のプログラマーには、推奨されないものの、必要なら非構造化プログラミングを選ぶ余地が与えられるという事実に関わっている。