2016年11月7日月曜日

開発環境

計算機プログラムの構造と解釈[第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.61、62.を取り組んでみる。

その他参考書籍

問題2.61、62.

コード(Emacs)

((lambda ()
  (load "procedures.scm")
  (newline)
  (define (p obj) (display obj) (newline))

  (define (element-of-set? x set)
    (if (null? set)
        #f
        (if (= x (car set))
            #t
            (if (< x (car set))
                #f
                (element-of-set x (cdr set))))))
  (define (adjoin-set x set)
    (if (null? set)
        (cons x set)
        (if (< x (car set))
            (cons x set)
            (if (= x (car set))
                set
                (cons (car set)
                      (adjoin-set x (cdr set)))))))

  (define set '(1 2 3 4 5 6 7 8 9 10))

  (p 2.61)
  (p (adjoin-set 0 set))
  (p (adjoin-set 5 set))
  (p (adjoin-set 100 set))

  (p 2.62)
  (define (union-set set1 set2)
    (if (null? set1)
        set2
        (if (null? set2)
            set1
            ((lambda (x1 x2)
               (if (< x1 x2)
                   (cons x1 (union-set (cdr set1) set2))
                   (if (= x1 x2)
                       (cons x1 (union-set (cdr set1) (cdr set2)))
                       (cons x2 (union-set set1 (cdr set2))))))
             (car set1) (car set2)))))

  (define odd '(1 3 5 7 9))
  (define even '(2 4 6 8 10))
  (p (union-set odd even))
  
  'done))

入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))

$ ksi < sample61.scm
ksi> 
2.61
(0 1 2 3 4 5 6 7 8 9 10)
(1 2 3 4 5 6 7 8 9 10)
(1 2 3 4 5 6 7 8 9 10 100)
2.62
(1 2 3 4 5 6 7 8 9 10)
=> done
ksi> $

0 コメント:

コメントを投稿