2014年9月12日金曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の4(超言語的抽象)、4.1(超循環評価器)、4.1.3(評価器のデータ構造)、述語のテスト、手続きの表現、環境に対する操作、問題 4.13.を解いてみる。

その他参考書籍

問題 4.13.

コード(BBEdit, Emacs)

sample13.scm

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

(define operations '())
(define (put op proc)
  (cons (list op proc) operations))
(define (get op)
  (define (inner operations)
    (if (null? operations)
        #f
        (let ((pair (car operations)))
          (if (eq? op (car pair))
              (cadr pair)
              (inner (cdr operations))))))
  (inner operations))
  
(define (eval exp env)
  (define (self-evaluating? exp)
    (cond ((number? exp) #t)
          ((string? exp) #t)
          (else #f)))
  (define (variable? exp) (symbol? exp))
  (define (application? exp) (pair? exp))
  (define (operator exp) (car exp))
  (define (operands exp) (cdr exp))
  (define (no-operands? ops) (null? ops))
  (define (first-operand ops) (car ops))
  (define (rest-operands ops) (cdr ops))
  
  (cond ((self-evaluating? exp) exp)
        ((variable? exp) (lookup-variable-value exp env))
        ((get (car exp))
         ((get (car exp)) exp env))
        ((application? exp)
         (apply (eval (operator exp) env)
                (list-of-values (operands exp) env)))
        (else
         (error "Unknown expression type -- EVAL" exp))))

(define (apply procedure arguments)
  (cond ((primitive-procedure? procedure)
         (apply-primitive-procedure procedure arguments))
        ((compound-procedure? procedure)
         (eval-sequence
          (procedure-body procedure)
          (extend-environment
           (procedure-parameters procedure)
           arguments
           (procedure-environment procedure))))
        (else
         (error
          "Unknown procedure type -- APPLY" procedure))))

;; 環境の最初のフレームからだけ結合を除去する
(define (install-unbind-package)
  (define (scan vars vals)
    (cond ((null? vars)
           (error "Unbound variable -- UNBIND!" var))
          ((eq? var (car vars))
           (set-car! vars (cadr vars))
           (set-cdr! vars (cddr vars))
           (set-car! vals (cadr vals))
           (set-cdr! vals (cddr vals)))
          (else (scan (cdr vars) (cdr vals)))))
  (define (eval-unbaind! exp env)
    (let ((first (first-frame env)))
      (scan (frame-variables frame)
            (frame-values frame))))

  (put 'unbind! eval-unbaind!))
                  
(install-unbind-package)

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

$ ./sample13.scm
$

0 コメント:

コメントを投稿