; CSE 341 lecture 18 (symbolic data) code ; A read-eval-print loop (REPL) that reads commands, runs them, and shows the result. (define (repl) (display "type something: ") (let ((code (read))) (display (eval code))) (newline) (repl)) ; differentiates the given function with respect to the given variable. ; differentiation rules: ; dc/dx = 0 ; dx/dx = 1 ; d(u+v)/dx = du/dx + dv/dx ; d(u*v)/dx = u(dv/dx) + v(du/dx) (define (deriv func var) (cond ((number? func) 0) ((symbol? func) (if (eq? var func) 1 0) ((sum? func) 3) (* unfinished *) ((product? func) 4) (* unfinished *) (else 0)))) (* unfinished *) ; (sum? '(+ 2 4)) -> #t ; (sum? 42) -> #f (define (sum? lst) (and (list? lst) (eq? '+ (car lst)))) ; (product? '(* 2 4)) -> #t ; (product? 42) -> #f (define (product? lst) (and (list? lst) (eq? '* (car lst)))) ; test functions for deriv procedure ; d/dx 42 -> 0 (define f0 42) ; d/dx (x+3) -> 1 (define f1 '(+ x 3)) ; d/dx (5x) -> 5 (define f2 '(* x 5)) ; d/dz (z^2 + 5z) -> 2z + 5 (define f3 '(+ (* z z) (* 5 z))) ; d/dx (ax^2 + bx + c) -> 2ax + b (define f4 '(+ (+ (* a (* x x)) (* b x)) c))