2014年1月4日土曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の5(レジスタ計算機での計算)、5.5(翻訳系)、翻訳系の概観、5.5.6(文面アドレス)、問題 5.41.を解いてみる。

その他参考書籍

問題 5.41.

コード(BBEdit)

sample.scm

(define (find-variable var compile-time-env)
  (define (iter frame-n disp-n frame frames)
    (if (null? frame)
        (if (null? frames)
            'not-found
            (iter (+ frame-n 1)
                  0
                  (car frames)
                  (cdr frames)))
        (if (eq? (car frame) var)
            (list frame-n disp-n)
            (iter frame-n
                  (+ disp-n 1)
                  (cdr frame)
                  frames))))
  (if (null? compile-time-env)
      'not-found
      (iter 0 0 (car compile-time-env) (cdr compile-time-env))))

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

$ scheme
MIT/GNU Scheme running under MacOSX
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Saturday October 26, 2013 at 11:02:50 PM
  Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/C 4.118
  Edwin 3.116

1 ]=> 
(define (find-variable var compile-time-env)
  (define (iter frame-n disp-n frame frames)
    (if (null? frame)
        (if (null? frames)
            'not-found
            (iter (+ frame-n 1)
                  0
                  (car frames)
                  (cdr frames)))
        (if (eq? (car frame) var)
            (list frame-n disp-n)
            (iter frame-n
                  (+ disp-n 1)
                  (cdr frame)
                  frames))))
  (if (null? compile-time-env)
      'not-found
      (iter 0 0 (car compile-time-env) (cdr compile-time-env))))

;Value: find-variable

1 ]=> (find-variable 'c '((y z) (a b c d e) (x y)))

;Value 2: (1 2)

1 ]=> (find-variable 'x '((y z) (a b c d e) (x y)))

;Value 3: (2 0)

1 ]=> (find-variable 'w '((y z) (a b c d e) (x y)))

;Value: not-found

1 ]=> ^D
End of input stream reached.
Moriturus te saluto.
$

0 コメント:

コメントを投稿