2013年7月10日水曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力, オブジェクトおよび状態)、3.3(可変データでのモデル化)、3.3.1(可変リストの構造)、共有と同一の問題 3.19を解いてみる。

その他参考書籍

問題3.19

コード(BBEdit)

sample.scm

(define (last-pair x)
  (if (null? (cdr x))
      x
      (last-pair (cdr x))))

(define (make-cycle x)
  (set-cdr! (last-pair x) x)
  x)

(define x1 (cons 'a (cons 'b (cons 'c '()))))

(define z (cons 'c '()))
(define y (cons 'b z))
(define x2 (cons z y))

(define z (cons 'a '()))
(define y (cons z z))
(define x3 (cons y y))

(define x4 (make-cycle (list 'a 'b 'c)))

(define (include-cycle? x)
  (define (iter x y)
    (cond ((eq? x y) true)
          ((or (null? x)
               (null? y)
               (null? (cdr y))) false)
          (else (iter (cdr x) (cddr y)))))
  (if (null? (cdr x))
      false
      (iter (cdr x) (cddr x))))

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

1 ]=> (include-cycle? x1)

;Value: #f

1 ]=> (include-cycle? x2)

;Value: #f

1 ]=> (include-cycle? x3)

;Value: #f

1 ]=> (include-cycle? x4)

;Value: #t

0 コメント:

コメントを投稿