計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
 - Emacs(Text Editor)
 - Scheme (プログラミング言語)
 - kscheme, Gauche (処理系)
 
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.3(記号データ)、2.3.3(例: 集合の表現)、問題2.60.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
 - プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
 - Scheme手習い
 
問題2.60.
コード(Emacs)
  (define equal?
  (lambda (a b)
    (if (and (pair? a) (pair? b))
        (and (eq? (car a) (car b))
             (equal? (cdr a) (cdr b)))
        (eq? a b))))
(define element-of-set?
  (lambda (x set)
    (cond ((null? set) #f)
          ((equal? x (car set)) #t)
          (else (element-of-set? x (cdr set))))))
(define adjoin-set
  (lambda (x set)
    (cons x set)))
(define intersection-set
  (lambda (set1 set2)
    (cond ((or (null? set1) (null? set2)) '())
          ((element-of-set? (car set1) set2)
           (cons (car set1)
                 (intersection-set (cdr set1) set2)))
          (else
           (intersection-set (cdr set1) set2)))))
(define union-set
  (lambda (set1 set2)
    (append set1 set2)))
(define s '(1 2 3 4 5))
(element-of-set? 1 s)
(element-of-set? 10 s)
(adjoin-set 10 s)
s
(intersection-set s '(6 7 8 9 10))
(intersection-set s s)
(intersection-set s '(1 3 5 7 9))
(union-set s '(6 7 8 9 10))
(union-set s s)
(union-set s '(1 3 5 7 9))
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ kscheme < sample60.scm kscm> kscm> kscm> kscm> kscm> kscm> kscm> #t kscm> #f kscm> (10 1 2 3 4 5) kscm> (1 2 3 4 5) kscm> () kscm> (1 2 3 4 5) kscm> (1 3 5) kscm> (1 2 3 4 5 6 7 8 9 10) kscm> (1 2 3 4 5 1 2 3 4 5) kscm> (1 2 3 4 5 1 3 5 7 9) kscm> $
0 コメント:
コメントを投稿