開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- MIT/GNU Scheme (処理系)
計算機プログラムの構造と解釈(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.5(翻訳したコードの例)、問題 5.36.を解いてみる。
その他参考書籍
問題 5.36.
翻訳系は組み合わせの被演算子は右から左の順の評価を作り出す。5.5.3(組合せの翻訳)にあるconstruct-arglistで右から左の順が決まる。
左から右の評価の順を作り出すように翻訳系を修正。
修正、追加箇所。
コード(BBEdit)
sample.scm
(define (construct-arglist operand-codes)
(if (null? operand-codes)
(make-instruction-sequence '() '(argl)
'((assign argl (const ()))))
(let ((code-to-get-first-argl
(append-instruction-sequences
(car operand-codes)
(make-instruction-sequence '(val) '(argl)
'((assign argl (op list) (reg val)))))))
(if (null? (cdr operand-codes))
code-to-get-first-arg
(preserving '(env)
code-to-get-first-arg
(code-to-get-rest-args
(cdr operand-codes)))))))
(define (code-to-get-rest-args operand-codes)
(let ((code-for-next-arg
(preserving '(argl)
(car operand-codes)
(make-instruction-sequence '(val argl) '(argl)
'((assign argl
(op adjoin-arg) (reg val) (reg argl)))))))
(if (null? (cdr operand-codes))
code-for-next-arg
(preserving '(env)
code-for-next-arg
(code-to-get-rest-args (cdr operand-codes))))))
左から右に組合せの被演算子を評価する場合、reverseが必要なくなるけど、appendを使うことになり、右からひだりに評価する場合に効率性が落ちる。
0 コメント:
コメントを投稿