2015年10月20日火曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs(Text Editor)
  • Scheme (プログラミング言語)
  • kscheme (github) (処理系)

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第3章(標準部品化力、オブジェクトおよび状態)、3.3(可変データのモデル化)、3.3.1(可変リスト構造)、共有と同一、問題3.18.を解いてみる。

その他参考書籍

問題3.18.

コード(Emacs)

(begin
  (define (print obj)
    (display obj)
    (newline))
  
  (define (cycle? lst)
    (define t (quote ()))
    (define (iter lst)
      (cond ((null? lst) #f)
            ((memq lst t) #t)
            (else (set! t (cons lst t))
                  (iter (cdr lst)))))
    (iter lst))

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

  (define x (list 1 2 3 4 5))
  (define cycle (make-cycle (list 1 2 3 4 5)))

  (print (cycle? x))
  (print (cycle? cycle)))

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

$ ./kscheme sample18.scm
#f
#t
$

0 コメント:

コメントを投稿