プログラミング再入門

プログラミングをもう一度ちゃんと勉強する読書ノート

SICP 1 Building Abstractions with Procedures

『Sceme手習い』『Scheme修行』と読んで、Schemeの基本的な事が分かった所で、巻末の推薦図書(?)に書いてある『Structure and Interpretation of Computer Programs』を読む事にしました。この本も実際の所Schemeに関する教科書ではないと思いますが、計算機科学の基礎を学べる有名な教科書なので、今後の糧に正に再入門として読もうと思います。日本語版は出ているのですが、あまり訳の評判が良くないので無料で公開されている原文(英語)で読むつもりです。

この教科書、各節の最後に問題が沢山出されていますが、最初のうち暫くは出て来ないので目に留まった文章をメモして行く事にします。

例や問題の実行にはDrRacketを使い、基本的にはR5RSモードで動かす事にします。

ノート

Chapter 1. Building Abstruction with Procedures

『building abstruction』で抽象化して行くと言う意味か。手続きによる抽象化、かな。

Well-designed computational systems, like well-designed automobiles or nuclear reactors, are designed in a modular manner, so that the parts can be constructed, replaced, and debugged separately.

うまく設計されていれば部品毎に分割された単位で構築され、交換可能で、個別にデバッグ出来る。

Programming in Lisp

Because the language possesses unique features that make it an excellent medium for studying important programming constructs and data structures and for relating them to the linguistic features that support them.

Lispがプログラミングの構成やデータ構造、それをプログラミング言語の機能と結びつけるのに最適だと言ってるのかな。

1.1 The Elements of Programming

In programming, we deal with two kinds of elements: procedures and data. (Later we will discover that they are really not so distinct.)

プログラミングでは手続きとデータを扱うが、後にその二つの区別は明確ではない事が分かる。

1.1.1 Expressions

expression = 式、かな。
示されている例を実際にDrRaket上で動かしてみる。

> 486
486
> (+ 137 349)
486
> (- 1000 334)
666
> (* 5 99)
495
> (/ 10 5)
2
> (+ 2.7 10)
12.7
> (+ 2.70 10)
12.7
> 

数値が小数部を含んでいる場合には結果も小数を含むが、その精度は入力の精度とは直接関係無いのかもしれない。

Expressions such as these, formed by delimiting a list of expressions within parentheses in order to denote procedure application, are called combinations.

リストで表された数式をコンビネーションと呼ぶ。

The value of a combination is obtained by applying the procedure specified by the operator to the arguments that are the values of the operands.

コンビネーション(数式)の値はオペレータにより指定される関数を、オペランドの値に適用する事で得られる。
『引数を関数に渡す』と言う表現とは感覚的に少し違うかも。

One of them is that it can accommodate procedures that may take an arbitrary number of arguments,

前置記法の利点は任意の数(特に二つより多い)の引数を取れる事。*1

> (+ 21 35 12 7)
75
> (* 25 4 12)
1200
> (- 10 1 2 3)
4
> 

最後の例は10 - 1 - 2 - 3となる事が分かる。

A second advantage of prefix notation is that it extends in a straightforward way to allow combinations to be nested, that is, to have combinations whose elements are themselves combinations:

前置記法だと入れ子にする事が出来る。
とあるが、オペランドに式が入れ子に入る事は特に前置記法固有の利点ではない気がする。

> (+ (* 3 5) (- 10 6))
19
> 

比較的簡単な方ではあるが人間には十分複雑な式の例:

> (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
57
> (+ (* 3
        (+ (* 2 4)
           (+ 3 5)))
     (+ (- 10 7)
        6))
57
> 

Observe in particular that it is not necessary to explicitly instruct the interpreter to print the value of the expression.

明示的に指定しなくてもインタープリタは式を評価した結果を表示する。

*1:後置記法だと更に括弧も要らなくなった様な。。。