Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define pi (acos -1))
- (define ^ expt)
- (define (plus args x)
- (define (iter S l)
- (if (null? l)
- S
- (let ((k (dif (car l) x)))
- (if (equal? k 0)
- (if (and (null? S) (null? (cdr l)))
- (list 0)
- (iter S (cdr l)))
- (iter (append S (list k)) (cdr l))) )))
- (let ((l (iter '() args)))
- (if (null? (cdr l))
- (car l)
- (cons '+ l))))
- (define (minus args x)
- (let ((l (if (not (null? (cdr args))) (dif (car args) x) 0))
- (r (if (not (null? (cdr args))) (plus (cdr args) x) (dif (car args) x)))
- )
- (if (equal? l 0)
- (if (equal? r 0)
- 0
- (list '- r)
- )
- (if (equal? r 0)
- l
- (list '- l r)
- )
- )
- )
- )
- (define (mult args x)
- (define (iter i S n)
- (define (probeg j l S)
- (if (> j n)
- S
- (let ((k (if (= i j)
- (dif (car l) x)
- (car l)
- )
- ))
- (case k (0 0)
- (1 (if (and (null? S) (null? (cdr l)))
- (list 1)
- (probeg (+ j 1) (cdr l) S)
- )
- )
- (else (probeg (+ j 1) (cdr l) (append S (list k))))
- )
- )
- )
- )
- (if (> i n)
- S
- (let ((k (probeg 1 args '())))
- (if (equal? k 0)
- (if (and (null? S) (= i n))
- (list 0)
- (iter (+ i 1) S n)
- )
- (if (= (length k) 1)
- (iter (+ i 1) (append S k) n)
- (iter (+ i 1) (append S (list (cons '* k))) n)
- )
- )
- )
- )
- )
- (let ((l (iter 1 '() (length args))))
- (if (null? (cdr l))
- (car l)
- (cons '+ l)
- )
- )
- )
- (define (div args x)
- (let* ((l (if (not (null? (cdr args))) (dif (car args) x) 0))
- (r (if (not (null? (cdr args))) (mult (cdr args) x) (dif (car args) x)))
- (l (if (list? l) l (list '* l)))
- (r (if (list? r) r (list '* r)))
- )
- (if (equal? l (list '* 0))
- (if (equal? r (list '* 0))
- 0
- (list '/ (list '- (append r (list (car args)))) (append (cons '^ (cdr args)) (list 2)))
- )
- (if (equal? r (list '* 0))
- (list '/ (append l (cdr args)) (append (cons '^ (cdr args)) (list 2)))
- (list '/ (list '- (append l (cdr args)) (append r (list (car args)))) (append (cons '^ (cdr args)) (list 2)))
- )
- )
- )
- )
- (define (exptdif args x)
- (let ((a (car args))
- (b (cadr args))
- )
- (if (number? b)
- (list '* b (dif a x) (list 'expt a (- b 1)))
- (list '* (list 'expt a (list '- b 1)) (list '+ (list '* b (dif a x))
- (list '* a (list 'log a) (dif b x))))
- )
- )
- )
- (define (dif f x)
- (cond ((equal? f x) 1)
- ((number? f) 0)
- ((list? f)
- (let ((op (car f))
- (args (cdr f))
- )
- (case op (+ (plus args x))
- (- (minus args x))
- (* (mult args x))
- (/ (div args x))
- (expt (exptdif args x))
- (^ (exptdif args x))
- (sin (list '* (cons 'cos args) (dif (car args) x)))
- (cos (list '* (list '- (cons 'sin args)) (dif (car args) x)))
- (log (list '/ (dif (car args) x) (car args)))
- (else 'unknown)
- )
- )
- )
- )
- )
- (define (derivative l)
- (dif l 'x)
- )
- (define (evaling l x value)
- (eval
- (list 'let (list (list x value)) l)
- (interaction-environment)
- )
- )
- (define (dift f x value)
- (evaling (dif f x) x value)
- )
- (define (tangent f x value)
- (list '+ (list '* (dift f x value) (list '- 'x value)) (evaling f x value))
- )#include <stdio.h>
Advertisement
Add Comment
Please, Sign In to add comment