SICP

3.5.4 Streams and Delayed Evaluation

遅延評価を(ストリームの内部だけでなく)ストリームの外で使ってみましょう、という話。遅延評価を使うとループのある回路を作れて、微分方程式も解ける。実行はGaucheを用いた。SICPの例では (define (solve f y0 dt) (define y (integral dy y0 dt)) (defi…

ながーい。 Formulating iterations as stream processes 3.63 以下のコードは何故効率が悪いのか?という問題。 (define (sqrt-stream x) (cons-stream 1.0 (stream-map (lambda (guess) (sqrt-improve guess x)) (sqrt-stream x))))メモ化してないから。 3…

3.5.2 Infinite Streams

SICP再開。がんばろー。結局ひげぽんさんに追い付けなかった。 無限ストリームです。 3.53 (define s (cons-stream 1 (add-streams s s)))はどのような結果を返すか頭で考えろ、という問題。 (stream-take s 10) ; => (1 2 4 8 16 32 64 128 256 512) 3.54 (…

3.5.1 Streams Are Delayed Lists

今までは"時間"の観点から現実世界をモデル化していたところを、別の視点からやってみましょう、というお話。具体的には(遅延)ストリームです。 前準備 サイトに載ってないコードだけ。 (define the-empty-stream '()) (define (stream-null? s) (eq? s the-…

3.4 Concurrency: Time Is of the Essence

久々に読む。なんか読みにくくて和訳版の本読んでみたけど同じくらいわかりにくかった…。 この節は多くの問題を省略。図を描いたりただ調べるだけのが多いので。 3.41 一つの処理しか行わない手続きを直列化することに意味はない。 3.42 よく分からない。問…

3.3.5 Propagation of Constraints

等式を制約として扱うことで解を求める。項どうしが演算、等号で結合されていればそれはそれらの項に対する制約と見なせる。たとえば a + b = c という式なら、どの2変数が決まっても残り1つの変数は自動的に決まる。これを自動化することを考えてる。おもし…

3.3.4 A Simulator for Digital Circuits

論理回路をシミュレート。 導線(wire)同士を関数で繋ぐ感じ。 入力側のwireの持つ値(導線の信号)が変化したときに関数を呼び出して、出力側のwireの信号を変化させる。 その際発生する遅延もシミュレートする。 時間軸を一列にとっておいて、after-delayとい…

3.3.3 Representing Tables

テーブル、連想リスト。 3.24 (same-key?を指定できるテーブル) (define (make-table same-key?) (let ((local-table (list '*table*))) (define (assoc key records) (cond ((null? records) #f) ((same-key? key (caar records)) (car records)) (else (as…

3.3.2 Representing Queues

(define (front-ptr queue) (car queue)) (define (rear-ptr queue) (cdr queue)) (define (set-front-ptr! queue item) (set-car! queue item)) (define (set-rear-ptr! queue item) (set-cdr! queue item)) (define (empty-queue? queue) (null? (front-pt…

3.3.1 Mutable List Structure

3.12 (define x (list 'a 'b)) (define y (list 'c 'd)) (define z (append x y)) z ; => (a b c d) (cdr x) ; => (b) (define w (append! x y)) w ; => (a b c d) (cdr x) ; => (b c d) 3.13 (define (last-pair x) (if (null? (cdr x)) x (last-pair (cdr …

3.2 The Environment Model of Evaluation

変数束縛と環境云々。問題は絵を描くのばっかなので理解したことにして省略。

3.1 Assignment and Local State

set!でデータの中身を変更できるようにすると参照透明性が失なわれてデータの同一性が判断できなくなるよ、というお話(かな?) 3.1 (define (make-accumulator acc) (lambda (n) (begin (set! acc (+ acc n)) acc))) 3.2 (define (make-monitored proc) (let…

2.5.3 Example: Symbolic Algebra

はついに怠け心が勝って飛ばすことに。

2.5.1 Generic Arithmetic Operations, 2.5.2 Combining Data of Different Types

じぇねりっく!じぇねりっく! コードがめちゃくちゃ長いです。 apply-genericでうまいことやってみよう。 前準備 (基本) (define (square x) (* x x)) (define-values (put get) (let ((*table* '())) (values (lambda (op type proc) (set! *table* (cons …

2.4 Multiple Representations for Abstract Data

斜め読みしてたらどこのコード使えばいいか分からなくなった。 データを複数のやり方で表現するよ。ちょうど複素数の直交座標・極座標表示が身近なのでその例を使います。 前準備 put,getは予めあるものとして進めているので(ずいぶん不親切だ)、適当に定義…

2.3.4 Example: Huffman Encoding Trees

ハフマン符号。面倒だなーって思ってたら前やった奴みつけたからそれを流用。 前準備 (define (make-leaf symbol weight) (list 'leaf symbol weight)) (define (leaf? object) (eq? (car object) 'leaf)) (define (symbol-leaf x) (cadr x)) (define (weigh…

2.3.3 Example: Representing Sets

データの集合をいろいろな方法で表現しましょう。 (define (element-of-set? x set) (cond ((null? set) #f) ((equal? x (car set)) #t) (else (element-of-set? x (cdr set))))) (define (adjoin-set x set) (if (element-of-set? x set) set (cons x set))…

2.3.2 Example: Symbolic Differentiation

シンボルを使って式の微分。 前準備 (define (deriv exp var) (cond ((number? exp) 0) ((variable? exp) (if (same-variable? exp var) 1 0)) ((sum? exp) (make-sum (deriv (addend exp) var) (deriv (augend exp) var))) ((product? exp) (make-sum (make…

2.3.1 Quotation

2.53 省略。 2.54 (define (%equal? a b) (cond ((and (pair? a) (pair? b)) (and (%equal? (car a) (car b)) (%equal? (cdr a) (cdr b)))) ((and (not (pair? a)) (not (pair? b))) (eq? a b)) (else #f))) 2.55 ''abracadabra ; => (quote abracadabra)だ…

2.2.4 Example: A Picture Language(あとで)

これhttp://d.hatena.ne.jp/motemen/20051226/1135612691でできると思ったんだけど、何だかbelowの定義が変(教科書通りにやっても違った絵が出る)なのとdraw-lineが定義されてなかったのとDrSchemeで実行するのがストレス溜まるのでスキップ。

2.2.3 Sequences as Conventional Interfaces

だんだん問題解くのが面倒になってきた。 前準備 (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) 2.33 (リストの基本関数をaccumulateで書き換え) (define (%map p …

2.2.1 Representing Sequences,2.2.2 Hierarchical Structures

nilの代わりに'()を使用する。 2.17 (define (last-pair lis) (if (null? (cdr lis)) lis (last-pair (cdr lis)))) 2.18 (define (reverse lis) (define (reverse-iter lis reversed) (if (null? lis) reversed (reverse-iter (cdr lis) (cons (car lis) rev…

2.1.1 Example: Arithmetic Operations for Rational Numbers

今までは手続きの抽象化、ここからはデータの抽象化。 リンク先のサイトの Figure 2.1: Data-abstraction barriers in the rational-number package が分かりやすい。 Programs that use rational numbers (Rational numbers in problem domain) add-rat sub…

1.3.4 Procedures as Returned Values

タイトル修正。 lambda 前準備 (define tolerance 0.00001) (define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) next (try n…

1.3.3 Procedures as General Methods

タイトル修正。 1.34 (define (f g) (g 2))MzSchemeで。 :mz (f f) procedure application: expected procedure, given: 2; arguments were: 2; (f f) ; => (f 2) ; => (2 2) 1.35 (fixed-pointで黄金比) (define tolerance 0.00001) (define (fixed-point f…

1.3.1 引数としての手続き

素人くさい読書会に行けないので自習する。と決めてから早速やってなかった…。 前準備 (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (integral f a b dx) (define (add-dx x) (+ x dx)) (* (sum f (+ a (/…

1.28

こんな感じかな。 (define (square x) (* x x)) (define (expmod base ex m) (cond ((= ex 0) 1) ((and (not (= base 1)) (not (= base (- m 1))) (= (remainder (square base) m) 1)) 0) ((even? ex) (remainder (square (expmod base (/ ex 2) m)) m)) (el…

日本語版買った

勉強会に向けて。所有欲とかが満たされる。計算機プログラムの構造と解釈作者: ジェラルド・ジェイサスマン,ジュリーサスマン,ハロルドエイブルソン,Gerald Jay Sussman,Julie Sussman,Harold Abelson,和田英一出版社/メーカー: ピアソンエデュケーション発…

2.4 Multiple Representations for Abstract Data

メモするの忘れてた。ここまで読んだ。様々な表現方法を持つデータを統一的に扱うのに、"Data Directed Style", "Message Passing Style"がある。前者はデータのタイプとメソッドから実際の関数を引くテーブルを用意、データにタイプを埋めこんどく。後者は…

2.3.4 Example: Huffman Encoding Trees

ここまでやった。ハフマン符号化。 Symbolic differentiation is of special historical significance in Lisp. It was one of the motivating examples behind the development of a computer language for symbol manipulation.