Advertisement
Guest User

D.scm

a guest
Jun 29th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 4.03 KB | None | 0 0
  1. (define (D y x)
  2.   (cond ((constant? y x) 0)
  3.         ((identity? y x) 1)
  4.         ((sum? y)
  5.          (let ((f (augend y)) (g (addend y)))
  6.            (make-sum (D f x) (D g x))))
  7.         ((product? y)
  8.          (let ((f (multiplier y)) (g (multiplicand y)))
  9.            (make-sum
  10.              (make-product (D f x) g)
  11.              (make-product f (D g x)))))))
  12.  
  13. (define (constant? expression variable)
  14.   (or (number? expression)
  15.       (and (symbol? expression)
  16.            (not (equal? expression variable)))))
  17.  
  18. (define (identity? expression variable)
  19.   (equal? expression variable))
  20.  
  21. ; sum
  22.  
  23. (define (sum? expression)
  24.   (and (list? expression)
  25.        (equal? (car expression) '+)))
  26.  
  27. (define (augend expression)
  28.   (apply make-sum (drop-right (cdr expression) 1)))
  29.  
  30. (define (addend expression) (last expression))
  31.  
  32. (define (make-sum . terms)
  33.   (let ((simplified-terms
  34.           (combine-like-terms
  35.             (delete
  36.               0
  37.               (rewrite
  38.                 number?
  39.                 (lambda (numbers) (apply + numbers))
  40.                 (rewrite
  41.                   sum?
  42.                   (lambda (sums) (append-map cdr sums))
  43.                   (map make-expression terms)))))))
  44.     (cond ((null? simplified-terms) 0)
  45.           ((null? (cdr simplified-terms))
  46.            (car simplified-terms))
  47.           (else `(+ ,@simplified-terms)))))
  48.  
  49. (define (combine-like-terms terms)
  50.   (if (< (length terms) 2)
  51.     terms
  52.     (rewrite
  53.       (lambda (term)
  54.         (or (equal? term 'x)
  55.             (and (product? term) (member 'x term))))
  56.       (lambda (variable-terms)
  57.         (hash-table-map
  58.           (fold-left
  59.             (lambda (coefficient-table term)
  60.               (receive
  61.                 (constants variables)
  62.                 (constants-variables term)
  63.                 (hash-table-update!/default
  64.                   coefficient-table
  65.                   variables
  66.                   (lambda (value) `(,@value ,@constants))
  67.                   '())
  68.                 coefficient-table))
  69.             (make-hash-table)
  70.             variable-terms)
  71.           (lambda (variables constants)
  72.             (apply make-product
  73.                    (apply make-sum constants)
  74.                    variables))))
  75.       terms)))
  76.  
  77. (define (constants-variables term)
  78.   (receive
  79.     (variables constants)
  80.     (partition
  81.       (lambda (factor) (equal? factor 'x))
  82.       (if (product? term) (cdr term) `(,term)))
  83.     (values
  84.       (if (null? constants) '(1) constants)
  85.       variables)))
  86.  
  87. (define (hash-table-map hash-table procedure)
  88.   (map (lambda (entry)
  89.          (procedure (car entry) (cdr entry)))
  90.        (hash-table->alist hash-table)))
  91.  
  92. ; product
  93.  
  94. (define (product? expression)
  95.   (and (list? expression)
  96.        (equal? (car expression) '*)))
  97.  
  98. (define (multiplier expression)
  99.   (apply make-product
  100.          (drop-right (cdr expression) 1)))
  101.  
  102. (define (multiplicand expression)
  103.   (last expression))
  104.  
  105. (define (make-product . factors)
  106.   (let ((simplified-factors
  107.           (delete
  108.             1
  109.             (rewrite
  110.               number?
  111.               (lambda (numbers) (apply * numbers))
  112.               (rewrite
  113.                 product?
  114.                 (lambda (products) (append-map cdr products))
  115.                 (map make-expression factors))))))
  116.     (cond ((null? simplified-factors) 1)
  117.           ((null? (cdr simplified-factors))
  118.            (car simplified-factors))
  119.           ((member 0 simplified-factors) 0)
  120.           (else `(* ,@simplified-factors)))))
  121.  
  122. ; auxiliaries
  123.  
  124. (define (make-expression expression)
  125.   (cond ((sum? expression)
  126.          (apply make-sum (cdr expression)))
  127.         ((product? expression)
  128.          (apply make-product (cdr expression)))
  129.         (else expression)))
  130.  
  131. (define (rewrite pattern substitution terms)
  132.   (receive
  133.     (matches nonmatches)
  134.     (partition pattern terms)
  135.     (if (null? matches)
  136.       nonmatches
  137.       (let ((result (substitution matches)))
  138.         `(,@nonmatches
  139.           ,@(if (list? result) result `(,result)))))))
  140.  
  141. (display (D '(* x x) 'x))
  142.  
  143. (newline)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement