2014年5月1日木曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.3(記号データ)、2.3.3(例: 集合の表現)、二進木としての集合、問題 2.64-a.を解いてみる。

その他参考書籍

問題 2.64-a.

作られる木の図。

コード(BBEdit, Emacs)

sample.scm

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

;; これまでに書いた手続き
(load "./tree.scm")

(define (display-indent n)
  (if (= n 0)
      (display "")
      (begin (display "        ")
             (display-indent (- n 1)))))

(define (print-tree tree indent)
  (if (null? tree)
      (begin (display-indent indent)
             (print "()"))
      (begin (print-tree (left-branch tree)
                         (+ indent 1))
             (begin (display-indent indent)
                    (print (entry tree)))
             (print-tree (right-branch tree)
                         (+ indent 1)))))
                         

(define tree (list->tree '(1 3 5 7 9 11)))

(print-tree tree 0)

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

$ ./sample.scm
                ()
        1
                        ()
                3
                        ()
5
                        ()
                7
                        ()
        9
                        ()
                11
                        ()
$

問題 2.64-a.

ステップ数の増加の程度はΘ(n)。

0 コメント:

コメントを投稿