Search code examples
schemeexponentiation

With Scheme language, how to check if a function is exponential


This shouldn't be this hard, but I'm stuck. We have a simple assignment where we're writing how to take the derivative of a function.

(define (derive exp var)
(cond ((number? exp) 0)
    ((variable? exp) (if (same-variable? exp var) 1 0))
    ((sum? exp) (derive-sum exp var))
    ((product? exp) (derive-product exp var))
    ((exponentiation? exp) (derive-exponentiation exp var))
    (else 'Error)))

But for the exponentiation, it needs to return true if I do have an exponential function. I'm just not entirely sure how to write it. So far I've just got something like this

(define (make-exponentiation base exponent)
   (cons base exponent)

(define (base exponentiation)
   (car exponentiation)

(define (exponent exponentiation)
   'cdr exponentiation)

(define (exponentiation? exp)
   'YourCodeHere)

(define (derive-exponentiation exp var)
   (* var (make-exponentiation exp (var-1)) (derive exp))

I'm not exactly sure what I'm checking about car and cdr. The whole thing is just a bit confusing. That's not the given code. I guess car and cdr are just kinda like placeholders at the moment.


Solution

  • It's quite some time, since I've done scheme. I'm more familiar with emacs lisp. So take with a grain of salt:

    (define (make-exponentiation base exponent)
       (list '^ base exponent))
    
    (define (base exponentiation)
       (car (cdr exponentiation)))
    
    (define (exponent exponentiation)
       (car (cdr (cdr exponentiation))))
    
    (define (exponentiation? exp)
       (equal? (car exp) '^))
    
    (define (derive-exponentiation exp var)
       (let ((b (base exp))
             (e (exponent exp)))
       (make-product e (make-exponentiation b (- e 1))))