1

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.

4

2 に答える 2

3

計画を立ててからかなり時間が経ちました。私はemacs Lispに精通しています。したがって、一粒の塩で服用してください:

(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))))
于 2012-11-02T19:19:30.827 に答える
1

あなたのコードは奇妙にフォーマットされているため、かなり読みにくいです。さまざまなものをさまざまな行に分けてみてください。Dr. Racket を使用している場合は、Tab キーを押すだけですべてが整列されますが、同じ行に複数のものがある場合は修正されません。

とにかく、この問題は、割り当ての設定方法と、どのような種類の入力を期待するかに大きく依存します。タグを使用することも、指数スポットに何かがあるかどうか、または 0 または 1 であるかどうかを確認することもできます。これも、入力がどのように与えられるかに完全に依存します。

于 2012-11-04T16:16:35.897 に答える