2014年4月23日水曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.3(記号データ)、2.3.2(例: 記号微分)、問題 2.57.を解いてみる。

その他参考書籍

問題 2.57.

コード(BBEdit, Emacs)

sample.scm

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

;; これまでに書いた手続き
(load "./procedures.scm")

(define (addend s) (cadr s))

(define (augend s)
  (if (null? (cdddr s))
      (caddr s)
      (cons '+
            (cddr s))))

(define (multiplier p) (cadr p))

(define (multiplicand p)
  (if (null? (cdddr p))
      (caddr p)
      (cons '*
            (cddr p))))

(define (deriv exp var)
  (cond ((number? exp) 0)
        ((variable? exp)
         (if (same-variable? exp
                             var)
             1
             0))
        ((sum? exp)
         (make-sum (deriv (addend exp)
                          var)
                   (deriv (augend exp)
                          var)))
        ((product? exp)
         (make-sum
          (make-product (multiplier exp)
                        (deriv (multiplicand exp)
                               var))
          (make-product (deriv (multiplier exp)
                               var)
                        (multiplicand exp))))
        ((exponentiation? exp)
         (let ((n (exponent exp))
               (u (base exp)))
           (make-product
            (make-product n
                          (make-exponentiation u
                                               (make-sum  n -1)))
            (deriv u var))))
        (else
         (error "unknown expression type -- DERIV" #?=exp))))
                              
(print (deriv '(* (* x y)
                  (+ x 3))
              'x))
(print (deriv '(+ (* 4 (* x x x))
                  (* 3 (* x x))
                  (* 2 x)
                  1)
              'x))

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

$ ./sample.scm
(+ (* x y) (* y (+ x 3)))
(+ (* 4 (+ (* x (+ x x)) (* x x))) (+ (* 3 (+ x x)) 2))
$

0 コメント:

コメントを投稿