計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力、オブジェクトおよび状態)、3.3(可変データでのモデル化)、3.3.4(デジタル回路のシミュレーター)、基本的な機能箱、問題 3.29.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.29.
コード(BBEdit, Emacs)
simulator_for_digital_circuits.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(define (inverter input output)
(define (inverter-input)
(let ((new-value (logical-not (get-signal input))))
(after-delay inverter-delay
(lambda ()
(set-signal! output new-value)))))
(add-action! input inverter-input)
'ok)
(define (logical-not s)
(cond ((= s 0) 1)
((= s 1) 0)
(else
(error "Invalid signal -- LOGICAL-NOT" s))))
(define (and-gate a1 a2 output)
(define (and-action-procedure)
(let ((new-value
(logical-and (get-signal a1) (get-signal a2))))
(after-delay and-gate-delay
(lambda ()
(set-signal! output new-value)))))
(and-action! a1 and-action-procedure)
(and-action! a2 and-action-procedure)
'ok)
(define (logical-and s1 s2)
(cond ((and (= s1 1) (= s2 1)) 1)
((or (= s1 0) (= s2 0)) 0)
(else
(error "Invalid signal -- LOGICAL-AND" s1 s2))))
(define (or-gate a1 a2 output)
(let ((a (make-wire))
(b (make-wire))
(c (make-wire)))
(inverter a1 a)
(inverter a2 b)
(and-gate a b c)
(inverter c output))
'ok)
;; このor-gateの遅延時間は、and-gate-delay 1個、inverter 3個分くらい
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./simulator_for_digital_circuits.scm $
0 コメント:
コメントを投稿