2015年10月16日金曜日

開発環境

  • 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.14.を解いてみる。

その他参考書籍

問題3.14.

コード(Emacs)

(begin
  (define (print obj)
    (display obj)
    (newline))

  (define (mystery x)
    (define (loop x y)
      (if (null? x)
          y
          (let ((temp (cdr x)))
            (set-cdr! x y)
            (loop temp x))))
    (loop x (quote ())))

  (define v (quote (a b c d)))
  (print v)                               ; (a b c d)
  
  (define w (mystery v))
  (print v)                               ; (a)
  (print w))                              ; (d c b a)

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

$ ./kscheme sample14.scm
(a b c d)
(a)
(d c b a)
$

0 コメント:

コメントを投稿