У меня есть рабочая функция, которая находит производную заданной функции по заданной переменной.У меня также есть функция упрощения (определение (упрощение expr), которая принимает выходные данные функции дифференцирования (определение (diff x expr) и упрощает ее в удобную для чтения форму. Я пытаюсь реализовать систему, которая позволяет пользователю вводитьвыражение, для которого они хотели бы найти производную и вернуть упрощенную производную. Что мне нужно изменить в 6-й строке кода, чтобы эта работа работала?
#racket
(display "Please enter an expression that you would like to differntiate: ")
(define expr (read))
(display expr)
(display " the derivative is ")
(display (simplify (diff 'x expr)))
(newline)
My simplify
и diff
функции
#lang racket/base
(define (diff x expr)
; differentiation function which gives derivative given a variable
(cond ((not (list? expr)) (if (equal? x expr) 1 0)) ; d/dx(x) = 1, d/dx(c) = 0
(else
(let ((operation (car expr)) ; expr is (operation u v)
(u (cadr expr))
(v (caddr expr)))
(case operation
((+) (list '+ (diff x u) (diff x v))) ; d/dx (u+v) = d/dx(u) + d/dx(v)
((-) (list '- (diff x u) (diff x v))) ; d/dx (u-v) = d/dx(u) - d/dx(v)
((*) (list '+ ; d/dx (u*v) = u * d/dxuv) + v * d/dx(u)
(list '* u (diff x v))
(list '* v (diff x u))))
((/) (list '/ (list '- ; power rule
(list '* v (diff x u))
(list '* u (diff x v)))))
((^) (list '* v (list '*
(list '^ u (- v 1))
(diff x u)))))))))
(define (simplify expr)
(if (not (list? expr))
expr
(let ((operation (car expr))
(a (simplify (cadr expr)))
(b (simplify (caddr expr))))
(case operation
((+) (cond ((and (number? a) (number? b)) (+ a b)) ;; use Racket's ability to add
((number? a)
(cond ((zero? a) (simplify b))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((-) (cond ((and (number? a) (number? b)) (- a b)) ;; use Racket's ability to subtract
((number? a)
(cond ((zero? a) (- (simplify b)))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((*) (cond ((and (number? a) (number? b)) (* a b)) ;; use Racket's ability to mulitpy
((number? a)
(cond ((zero? a) 0)
((= a 1) (simplify b))
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) 0)
((= b 1) (simplify a))
(else (list operation (simplify a)(simplify b)))))
(else (list operation (simplify a) (simplify b)))))
((/) (cond ((and (number? a) (number? b)) (/ a b)) ;; use Racket's ability to divide
((number? a)
(cond ((zero? a) 0)
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) (error "Divison by 0, statement undefined!"))
((= b 1) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else
(list operation (simplify a) (simplify b)))))
((^) (cond ((and (number? a) (number? b)) (expt a b)) ;; use Racket's ability to exponentiate
((number? a)
(cond ((= a 1) 1) ;; ((zero? a) 0) ;; depends on b [b < 0 then undefined]
(else (list operation (simplify a) (simplify b)))))
((number? b)
(cond ((zero? b) 1) ;; depends on a [a = 0 then undefined]
((= b 1) (simplify a))
(else (list operation (simplify a) (simplify b)))))
(else (list operation (simplify a) (simplify b)))))))))