首页 > 其他分享 >sicp每日一题[2.56]

sicp每日一题[2.56]

时间:2024-10-24 08:52:43浏览次数:1  
标签:2.56 exponentiation make sicp number base exp 每日 exponent

Exercise 2.56

Show how to extend the basic differentiator to handle more kinds of expressions. For instance, implement the differentiation rule

d(x^n)/dx = n x^(n-1)

by adding a new clause to the deriv program and defining appropriate procedures exponentiation?, base, exponent, and make-exponentiation.
(You may use the symbol ** to denote exponentiation.) Build in the rules that anything raised to the power 0 is 1 and anything raised to the power 1 is the thing itself.


这道题难度不大,先仿照 make-sum, make-product 写出 make-exponentiation 函数,剩下的部分就很简单了。

(define (make-exponentiation base exponent)
  (cond ((=number? base 0) 0)
        ((=number? base 1) 1)
        ((and (number? base) (=number? exponent 0)) 1)
        ((and (number? base) (=number? exponent 1)) base)
        ((and (number? base) (number? exponent)) (* base (make-exponentiation base (- exponent 1))))
        (else (list '** base exponent))))

(define (exponentiation? x) (and (pair? x) (eq? (car x) '**)))

(define (base e) (cadr e))

(define (exponent e) (caddr e))

(define (deriv exp var)
  (cond ((number? exp) 0)
        ((variable? exp) (if (same-variable? exp var) 1 0))
        ((exponentiation? exp)
         (let ((base (base exp))
               (n (exponent exp)))
           (if (same-variable? base var)
               (make-product n
                             (make-exponentiation base (- n 1)))
               0)))
        ((sum? exp) (make-sum (deriv (addend exp) var)
                              (deriv (augend exp) var)))
        ((product? exp)
         (make-sum
          (make-product (multiplier exp)
                        (deriv (multiplicand exp) var))
          (make-product (deriv (multiplier exp) var)
                        (multiplicand exp))))
        (else
         (error "unknown expression type: DERIV" exp))))


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

; 执行结果
'(* 3 (** x 2))
0

标签:2.56,exponentiation,make,sicp,number,base,exp,每日,exponent
From: https://www.cnblogs.com/think2times/p/18498748

相关文章

  • 每日一题:Leetcode-316 去除重复字母
    力扣题目解题思路java代码力扣题目:给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。示例1:输入:s="bcabc"输出:"abc"示例2:输入:s="cbacdcbc"输出:"acdb"提示:1<=s.l......
  • 每日一个Mac小技巧推荐之你不知道的常用快捷键
    Mac上有着非常多的快捷键,但是大部分人应该只知道使用复制粘贴,但其实快捷键的设计本身就是为了提高使用效率,只是查找起来非常的麻烦因此不被选择,今天为大家列举一些你不知道的快捷键,同时教大家如何能够快速的查看快捷键快速锁屏:control+command+Q退出应用程序:command+Q强制退......
  • 每日算法一练:剑指offer——数组篇(4)
    数据流中的中位数        中位数 是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是两个中间值的平均值。例如,[2,3,4] 的中位数是 3[2,3] 的中位数是 (2+3)/2=2.5设计一个支持以下两种操作的数据结构:voidaddNum(intnum) -从数据......
  • 20241022每日一题洛谷P1223
    普及洛谷P1223接水问题有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小第一行为一个整数n,第二行n个整数,第i个整数Ti表示第i个人的接水时间Ti输出两行,第一行为一种平均时间最短的排队顺......
  • sicp每日一题[2.52]
    Exercise2.52MakechangestothesquarelimitofwaveshowninFigure2.9byworkingateachofthelevelsdescribedabove.Inparticular:a.AddsomesegmentstotheprimitivewavepainterofExercise2.49(toaddasmile,forexample).b.Changethe......
  • 2024-10-21每日一题
    P1223排队接水题目描述有\(n\)个人在一个水龙头前排队接水,假如每个人接水的时间为\(T_i\),请编程找出这\(n\)个人排队的一种顺序,使得\(n\)个人的平均等待时间最小。输入格式第一行为一个整数\(n\)。第二行\(n\)个整数,第\(i\)个整数\(T_i\)表示第\(i\)个人的......
  • 1107. 每日新用户统计
    力扣题目跳转(1107.每日新用户统计-力扣(LeetCode))Traffic 表:+---------------+---------+|ColumnName|Type|+---------------+---------+|user_id|int||activity|enum||activity_date|date|+---------------+---------+......
  • 10.21日每日收获
    1、扇区擦除时按首地址擦除,若设定地址不是首地址也从首地址开始擦除,每512个字节为一组,如00H-200H为一组,200H-400H为一组,擦除数据时按组擦除,若果设置擦除开始地址为100h,则仍然会从00H-200H擦除,而不是100H-300H2、有些芯片的FLASHROM结构是类RAM结构,也就是无需擦除可以直接覆盖......
  • 每日一道算法题(栈)
     What'spastisprologue. 凡是过去,皆为序章。题目 分析 1.我们可以用栈的结构来解决这道题。2.我们使用while循环,每次读取字符串中一个元素进行操作,直到最后读取到'\0'为止。3.如果遇见'(','[','{' 这三种左括号,则把该左括号入栈;如果遇见')',']','}......
  • sicp每日一题[2.51]
    Exercise2.51Definethebelowoperationforpainters.belowtakestwopaintersasarguments.Theresultingpainter,givenaframe,drawswiththefirstpainterinthebottomoftheframeandwiththesecondpainterinthetop.Definebelowintwodiffere......