;;; (factorial n) returns n factorial, for n>=0. (define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1))))) ;;; Same function using tail-recursion (= iteration) (define (factorial n) (define (f n val) (if (= n 0) val (f (- n 1) (* val n)))) (f n 1)) ;;; (map f vals) returns the list resulting from ;;; applying function f to each value in the list values. (define (map f vals) (if (null? vals) '() (cons (f (first vals)) (map f (rest vals))))) ;;; (deriv exp var) returns the result of differentiating ;;; the symbolic expression exp wrt the variable var. (define (deriv exp var) (cond ((number? exp) 0) ((variable? exp) (if (same-variable? exp var) 1 0)) ((sum? exp) ; e.g., (+ 2 x (* 3 y)) (make-sum (map (lambda (exp) (deriv exp var)) (summands exp)))) ((product? exp) ; e.g., (* (+ x 1) (* (+ 2 y) 3)) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (deriv (multiplier exp) var) (multiplicand exp)))) (else (error "unknown expression type -- DERIV" exp))))