sicp每日一题[2.57]

Exercise 2.57

Extend the differentiation program to handle sums and products of arbitrary numbers of (two or more) terms. Then the last example above could be expressed as

(deriv '(* x y (+ x 3)) 'x)

Try to do this by changing only the representation for sums and products, without changing the deriv procedure at all. For example, the addend of a sum would be the first term,
and the augend would be the sum of the rest of the terms.


这道题挺难的,我理解了题目提示的方法,就是把剩下的项也表示为和或者乘的形式,但是我不明白该怎么实现,递归调用并不能达到要求,最后上网搜了一下才知道怎么做。

(define (augend s)
  (let ((last (cddr s)))
    (if (null? (cdr last))
        (car last)
        (cons '+ last))))

(define (multiplicand p)
    (let ((last (cddr p)))
    (if (null? (cdr last))
        (car last)
        (cons '* last))))

(deriv '(* x y (+ x 3)) 'x)
(deriv '(+ x (* 3 (+ x (+ y 2)))) 'x)
(deriv '(+ x (* 3 (+ x y 2))) 'x)

; 执行结果
'(+ (* x y) (* y (+ x 3)))
4
4
posted @   再思即可  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示