Asst 2 Starting Code
; Modified d function to add a case for the Ax^n expression. (load "diff.lsp") ; Create new version of d which adds a case for the Ax^n type expression. ; The function Axn? detects such an expression, and diff-Axn executes the ; rule. (define (d x E) (cond ((constant? E) (diff-constant x E)) ((variable? E) (diff-variable x E)) ((sum? E) (diff-sum x E)) ((Axn? x E) (diff-Axn x E)) ; New case here. ((product? E) (diff-product x E)) (#t (error ERR_BADEXPR "Cannot parse expression.")) ) ) ; Takes an expression, and collects all the constants into a single value ; at the front. Assumes E is an add or multiply expression (a list starting ; with + or *) All the constants will be removed, combined by the operator, ; and left at the front. If the original list contains no constants, the ; identity will be added to the front of the list. (define (collect-constants E) (let ( (parts (cdr E)) (const (eval (cons (car E) (select constant? parts)))) (non-const (select (lambda (x) (not (constant? x))) parts)) ) (cons (car E) (cons const non-const)) ) ) ; Returns true if all the members of the list are equal. Returns true for ; empty and singleton lists. (define (all-the-same? lst) (cond ((null? lst) #t) ; empty list ((null? (cdr lst)) #t) ; singleton list. (#t (reduce and (map (lambda (x) (equal? x (car lst))) (cdr lst)) #t ) ) ) ) ; Detect that an expression has the form Ax^n. That is, it is a product ; of constants (zero or more) and the variable x (one or more). (define (Axn? x E) ; Give this one a real body nil ) ; Take the derivative of an expression that is assumed to satisfy the ; Axn? function given above. (define (diff-Axn x E) ; This needs one, too. (error 100 "diff-Axn isn't done yet.") )