如果你多次提供它,递归如何在sicp讲座3b上工作?

时间:2014-07-22 14:28:05

标签: scheme

我在这里查看关于计算代数表达式的推导的课程:

https://www.youtube.com/watch?v=X21cKVtGvYk

我理解它是如何工作的,但它如何找到像ax ^ 3 + bx ^ 2 + cx这样的表达式的派生,因为它有3个子部分可以解决?

(define (deriv exp var)
    (cond
        ((CONST? exp var) 0)
        ((SAME-VAR? exp var) 1)
        ((SUM? exp)
            (make-sum
                (deriv (A1 exp) var)
                (deriv (A2 exp) var)))
        ((PRODUCT? exp)
            (make-sum
                (make-product
                    (M1 exp)
                    (deriv (M2 exp) var))
                (make-product
                    (deriv (M1 exp) var)
                    (M2 exp))))
        ; ... more rules
        )
    )
;# END BOARD



;# BOARD 0:14:50
(define (CONST? exp var)
    ; An expressin is constant if:
    ; - I cannot break it up into more primitive pieces
    ; - It's not var
    (AND
        (ATOM? exp)
        (NOT (EQ? exp var)))
    )

(define (SAME-VAR? exp var)
    (AND
        (ATOM? exp)
        (EQ? exp var))
    )
;# END BOARD



;# BOARD 0:17:35
(define (SUM? exp)
    ; An expression is a sum if its first element equals '+
    (AND
        (NOT (ATOM? exp))
        (EQ (CAR exp) '+)) ; Notice the quotation.
    )

(define (make-sum a1 a2)
    (LIST '+ a1 a2))

(define a1 cadr)
;# END BOARD

1 个答案:

答案 0 :(得分:0)

ax ^ 3 + bx ^ 2 + cx将表示为(ax ^ 3 + bx ^ 2)+ cx。所以这是两个元素的两个和。