Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.07 KB | None | 0 0
  1. (define (make-list n el)
  2.   (cond
  3.     ((= n 0) '())
  4.     ((= n 1) (list el))
  5.     (else (cons el (make-list (- n 1) el)))))
  6.  
  7. (define (mul-diff-term term sym)
  8.   (if (= (cadr term) (caddr term))
  9.       (diff (car term) sym)
  10.       (car term)))
  11.  
  12. (define (range i j)
  13.   (if (= i j)
  14.       (list i)
  15.       (cons i (range (+ i 1) j))))
  16.  
  17. (define (zip as bs cs)
  18.   (map list as bs cs))
  19.  
  20. (define (gen-mul-terms terms)
  21.   (map (lambda (t) (zip terms (make-list (length terms) t) (range 1 (length terms)))) (range 1 (length terms))))
  22.    
  23.    
  24.  
  25. (define (diff expr sym)
  26.   (if (not (list? expr))
  27.       (if (eq? expr sym)
  28.           1
  29.           0)
  30.       (let ((op (car expr))
  31.             (args (cdr expr)))
  32.  
  33.            (cond
  34.              ((eq? op '+) (cons '+ (map (lambda (f) (diff f sym)) args)))
  35.              ((eq? op '*) (cons '+ (map (lambda (terms) (cons '* (map (lambda (term) (mul-diff-term term sym)) terms))) (gen-mul-terms args))))
  36.              (else '?))))
  37.   )
  38. ;(cons '+ (map (lambda (t) (cons '* (map (lambda (s) (mul-diff-term s 'x)) t))) (gen-mul-list '(x x y))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement