開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Scheme (プログラミング言語)
- kscheme (ksi)(github) (処理系)
計算機プログラムの構造と解釈[第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.59、60.を取り組んでみる。
その他参考書籍
問題2.59、60.
コード(Emacs)
(begin (load "procedures.scm") (newline) (define (p obj) (display obj) (newline)) (define (element-of-set? x set) (if (null? set) #f (if (equal? x (car set)) #t (element-of-set? x (cdr set))))) (define (adjoin-set x set) (if (element-of-set? x set) set (cons x set))) (define (intersection-set set1 set2) (if (or (null? set1) (null? set2)) '() (if (element-of-set? (car set1) set2) (cons (car set1) (intersection-set (cdr set1) set2)) (intersection-set (cdr set1) set2)))) (define (union-set set1 set2) (if (null? set1) set2 (adjoin-set (car set1) (union-set (cdr set1) set2)))) (p 2.59) (p (union-set '() '())) (p (union-set '() '(1))) (p (union-set '(1) '())) (p (union-set '(1) '(1))) (p (union-set '(1 2) '(3 4 5))) (p (union-set '(1 2 3 4) '(4 5))) (p 2.60) (p 'before-intersection) (p (intersection-set '() '())) (p (intersection-set '() '(1))) (p (intersection-set '(1) '())) (p (intersection-set '(1) '(1))) (p (intersection-set '(1 2) '(3 4 5))) (p (intersection-set '(1 2 3 4) '(4 5))) (define (adjoin-set x set) (cons x set)) (p 'after-intersection) (p (intersection-set '() '())) (p (intersection-set '() '(1))) (p (intersection-set '(1) '())) (p (intersection-set '(1) '(1))) (p (intersection-set '(1 2) '(3 4 5))) (p (intersection-set '(1 2 3 4) '(4 5))) (p 'after-union) (p (union-set '() '())) (p (union-set '() '(1))) (p (union-set '(1) '())) (p (union-set '(1) '(1))) (p (union-set '(1 2) '(3 4 5))) (p (union-set '(1 2 3 4) '(4 5))) 'done)
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ ksi < sample59.scm ksi> 2.59 () (1) (1) (1) (1 2 3 4 5) (1 2 3 4 5) 2.6 before-intersection () () () (1) () (4) after-intersection () () () (1) () (4) after-union () (1) (1) (1 1) (1 2 3 4 5) (1 2 3 4 4 5) => done ksi> $
0 コメント:
コメントを投稿