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

sicp每日一题[2.58]

时间:2024-10-26 13:09:06浏览次数:1  
标签:last 每日 2.58 sicp number a2 m1 m2 define

Exercise 2.58

Suppose we want to modify the differentiation program so that it works with ordinary mathematical notation, in which + and * are infix rather than prefix operators. Since the differentiation program is defined
in terms of abstract data, we ca modify it to work with different representations of expressions solely by changing the predicates, selectors, and constructors that define the representation of the algebraic
expressions on which the differentiator is to operate.

a. Show how to do this in order to differentiate algebraic expressions presented in infix form, such as (x + (3 * (x + (y + 2)))). To simplify the task, assume that + and * always take two arguments and that
expressions are fully parenthesized.
b. The problem becomes substantially harder if we allow standard algebraic notation, such as (x + 3 * (x + y + 2)), which drops unnecessary parentheses and assumes that multiplication is done before addition.
Can you design appropriate predicates, selectors, and constructors for this notation such that our derivative program still works?


这道题乍一看很棘手,但是仔细一想其实挺简单的,只要把构造函数和选择器里 + 和 * 的位置改一下就行了,还有原来的前缀表示法要多加一个 + 或 *,现在也可以直接省略掉了。

(define (make-sum a1 a2)
  (cond ((=number? a1 0) a2)
        ((=number? a2 0) a1)
        ((and (number? a1) (number? a2)) (+ a1 a2))
        (else (list a1 '+ a2))))

(define (sum? x) (and (pair? x) (eq? (cadr x) '+)))

(define (addend s) (car s))

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

(define (make-product m1 m2)
  (cond ((or (=number? m1 0) (=number? m2 0)) 0)
        ((=number? m1 1) m2)
        ((=number? m2 1) m1)
        ((and (number? m1) (number? m2)) (* m1 m2))
        (else (list m1 '* m2))))

(define (product? x) (and (pair? x) (eq? (cadr x) '*)))

(define (multiplier p) (car p))

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


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

; 执行结果
4
4

标签:last,每日,2.58,sicp,number,a2,m1,m2,define
From: https://www.cnblogs.com/think2times/p/18503959

相关文章

  • 每日OJ题_牛客_NC383主持人调度(一)_排序​_C++_Java
    目录牛客_NC383主持人调度(一)_排序题目解析C++代码Java代码牛客_NC383主持人调度(一)_排序主持人调度(一)_牛客题霸_牛客网(nowcoder.com)描述:        有n 个活动即将举办,每个活动都有开始时间与活动的结束时间,第i 个活动的开始时间是starti ,第i 个活动......
  • 20241024每日一题洛谷P1012
    普及-洛谷P1012拼数设有n个正整数,a1a2a3......an将它们联接成一排,相邻数字首尾相接,组成一个最大的整数输入:第一行有一个整数,表示数字个数n第二行有n个整数,表示给出的n个整数ai输出:一个正整数,表示最大的整数可以考虑两种路线:使用sort函数编辑cmp参数进行字符串......
  • 2024-10-23-leetcode每日一题-构成整天的下标对数目 II
    题目描述给你一个整数数组 hours,表示以 小时 为单位的时间,返回一个整数,表示满足 i<j 且 hours[i]+hours[j] 构成 整天 的下标对 i, j 的数目。整天 定义为时间持续时间是24小时的 整数倍 。例如,1天是24小时,2天是48小时,3天是72小时,以此类推。......
  • 每日OJ题_牛客_DP10最大子矩阵_二维前缀和_C++_Java
    目录牛客_DP10最大子矩阵_二维前缀和题目解析C++代码Java代码牛客_DP10最大子矩阵_二维前缀和最大子矩阵_牛客题霸_牛客网(nowcoder.com)描述:        已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵,你的任务是找到最大的非空(大小至少是1*1)子矩......
  • 【每日一题】LeetCode - 最长回文子串
    在字符串相关的算法题中,寻找最长回文子串是一个经典且富有挑战性的题目。本篇将详细分析并推导两种有效的解决方案:动态规划法和中心扩展法。题目描述给定一个字符串s,我们需要找到s中最长的回文子串。回文是指正着读和反着读都相同的字符串。例如,输入"babad"时,输出可......
  • 10.24每日总结:程序员修炼之道读后感1
    首次读《程序员修炼之道:从小工到专家》,我深受启发。这本书犹如一盏明灯,为程序员的成长之路指明了方向。在书中,作者强调了许多重要的理念和实践方法。其中,对我触动最深的是关于代码质量的重视。优秀的程序员不仅要追求代码的功能性,更要注重代码的可读性、可维护性和可扩展性。正如......
  • Leetcode每日一题:3175. 找到连续赢 K 场比赛的第一位玩家
    题意为给定一个数组,数组头两数比大小,大的放队首,小的放队尾,找到能够连续赢K次的数的编号。思路:如果一轮比较完了,没有赢K次的,那答案就是数组最大值。利用双指针,模拟一轮比较,计算每个数的胜利次数,注意,若起点不是0的话,意味着他和之前的数比较是胜出的,所以初始为1,否则初始为0;1clas......
  • sicp每日一题[2.56]
    Exercise2.56Showhowtoextendthebasicdifferentiatortohandlemorekindsofexpressions.Forinstance,implementthedifferentiationruled(x^n)/dx=nx^(n-1)byaddinganewclausetothederivprogramanddefiningappropriateproceduresexpone......
  • 每日一题:Leetcode-316 去除重复字母
    力扣题目解题思路java代码力扣题目:给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。示例1:输入:s="bcabc"输出:"abc"示例2:输入:s="cbacdcbc"输出:"acdb"提示:1<=s.l......
  • 每日一个Mac小技巧推荐之你不知道的常用快捷键
    Mac上有着非常多的快捷键,但是大部分人应该只知道使用复制粘贴,但其实快捷键的设计本身就是为了提高使用效率,只是查找起来非常的麻烦因此不被选择,今天为大家列举一些你不知道的快捷键,同时教大家如何能够快速的查看快捷键快速锁屏:control+command+Q退出应用程序:command+Q强制退......