当前位置: 首页 > news >正文

算术包实例:符号代数练习题

练习2.87 以下将对多项式进行算术包构建:

点击查看代码
;;通用操作
(define (add x y) (apply-generic 'add x y))
(define (mul x y) (apply-generic 'mul x y))
(define (=zero? x) (apply-generic '=zero? x))
;;多项式运算中涉及的算术操作包
;;数字部分
(define (install-scheme-number-package)(define (tag-args op)(lambda (x y) (attach-tag 'scheme-number (op x y))))(put 'add '(scheme-number scheme-number) (tag-args +))(put 'mul '(scheme-number scheme-number) (tag-args *))(put '=zero? '(scheme-number)(lambda (x) (= x 0))))
;;符号代数部分
(define (install-polynomial-package)(define (number->poly variable n)(make-poly variable (adjoin-term (make-term 0 n) (the-empty-termlist))))(define (tag x) (attach-tag 'polynomial' x))(define (put-op name op)(put 'name '(polynomial polynomial)(lambda (x y) (tag (op x y))))(put 'name '(polynomial scheme-number)(lambda (x y) (tag (op x (number->poly (variable x) y)))))(put 'name '(scheme-number polynomial)(lambda (x y) (tag (op (number->poly (variable y) x)) y))))(put-op 'add add-poly)(put-op 'mul mul-poly)(put '=zero? 'polynomial =zero-poly?)'done)
;;对符号代数中需要的函数进行说明
(define (polynomial? p)(and (pair? p) (eq? (car p) 'polynomial)))
(define (make-poly variable term-list)(cons 'polynomial (cons variable term-list)))
(define (variable? p) (symbol? p))
(define (same-variable? p1 p2)(and (variable? p1) (variable? p2)(eq? p1 p2)))
(define (variable p)(cadr p))
(define (term-list p)(cddr p))
(define (add-poly p1 p2)(if (same-variable? (variable p1) (variable p2))(make-poly (variable p1)(add-terme (term-list p1)(term-list p2)))(error "Polys not in same var -- ADD" (list p1 p2))))
(define (mul-poly p1 p2)(if (same-variable? (variable p1) (variable p2))(make-poly (variable p1)(mul-terme (term-list p1)(term-list p2)))(error "Polys not in same var -- MUL" (list p1 p2))))
(define (add-terms L1 L2)(cond ((empty-termlist? L1) L2)((empty-termlist? L2) L1)(else(let ((t1 (first-term L1))(t2 (firet-term L2)))(cond ((> (order t1) (order t2))(adjoin-term t1 (add-terms (rest-terms L1) L2)))((> (order t2) (order t1))(adjoin-term t2 (add-terms L1 (rest-terms L2))))(else (adjoin-term (make-term (order t1) (add (coeff t1) (coeff t2)))(add-terms (rest-terms L1) (rest-terms L2)))))))))
(define (mul-terms L1 L2)(if (empty-termlist? L1)(the-empty-termlist)(add-terms (mul-terms-by-all-terms (first-term L1) L2)(mul-terms (rest-terms L1) L2))))
(define (mul-terms-by-all-terms t1 L)(if (empty-termlist? L)(the-empty-termlist)(let ((t2 (first-term L)))(adjoin-term(make-term (+ (order t1) (order t2))(mul (coeff t1) (coeff t2)))(mul-terms-by-all-terms t1 (rest-terms L))))))
(define (adjoin-term term term-list)(if (=zero? (coeff term))term-list(cons term term-list)))
;;零的判断
(define (=zero-poly? poly)(define (coeff-all-zero? term-list)(if (empty-termlist? term-list) #t(if (=zero? (coeff (first-term term-list)))(coeff-all-zero? (rest-of-terms term-list))#f)))(coeff-all-zero? (term-list poly)))
(define (first-term term-list) (car term-list))
(define (rest-of-terms term-list) (cdr term-list))
(define (make-term oredr coeff)(cond oredr coeff))
(define (order term)(car term))
(define (coeff term)(cadr term))
(define (empty-termlist? term-list)(null? term-list))

练习2.88 扩充算术包,加上多项式的减法。

点击查看代码
;;整数包中添加:
(put 'neg '(scheme-number) (lambda (x) (tag (- x))))
;;多项式中添加:
(put-op 'sub sub-poly)
(put 'neg '(polynomial) (lambda (x) (tag (neg-poly x))))
;;定义减法:
(define (sub-poly p1 p2)(add p1 (neg p2)))
;;通过一个通用的取负操作实现减法
(define (neg-poly p)(make-poly (variable p)(neg-term (term-list p))))
(define (neg-term L)(if (empty-termlist? L)(the-empty-termlist)(let ((t (first-term L)))(adjoin-term (make-term (order t) (neg (coeff t)))(neg-term (rest-of-terms L))))))
http://www.jsqmd.com/news/269766/

相关文章:

  • 导师严选9个一键生成论文工具,专科生毕业论文轻松搞定!
  • 大模型部署测试
  • DAY45@浙大疏锦行
  • 不会建模也能做 3D?2D 原画“充气”变动画的逃课流
  • 想在 Java 八股文面试中脱颖而出?这1000 道互联网大厂面试题必不可少!
  • 9个高效降aigc工具推荐,本科生必看!
  • 啃完阿里老哥这套Java面试八股文后,成功收获蚂蚁 offer
  • 源自新西兰的天然馈赠:Newo纽渥有机娟姗鲜牛奶,重新定义家庭健康饮奶标准 - 行业调研院
  • 致并肩前行的你:一封来自近屿智能的信
  • day154—回溯—分割回文串(LeetCode-131)
  • OpenAI和Anthropic竞相布局医疗健康领域,AI医疗浪潮已至
  • day155—回溯—组合(LeetCode-77)
  • 实用指南:零基础学AI大模型之MultiQueryRetriever多查询检索全解析
  • 基于Hough变换的答题卡识别MATLAB之旅
  • 计算机小程序毕设实战-基于django+微信小程序的运动饮食健康生活系统【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • Day23-20260119
  • C# 实现 TCP/IP 客户端与服务器数据交互及与西门子 S7 - 200Smart 通讯
  • 2026大专计算机专业学数据分析的价值分析
  • PySide系列-07-QMainWindow
  • 【计算机毕业设计案例】基于微信小程序的考研资源共享平台的设计与实现基于django+微信小程序的考研信息查询系统(程序+文档+讲解+定制)
  • c++中的常用栈操作
  • 2026/1/17-Atcoder Beginner Contest 441 T1~4
  • 群友靶机lara复现 - 场
  • 小程序毕设选题推荐:基于django+微信小程序的健康生活系统个人健康生活平台小程序【附源码、mysql、文档、调试+代码讲解+全bao等】
  • 信件分析(2)
  • 探索人脸识别追踪:从图像采集到电机驱动的奇妙旅程
  • ​​​​​​​推荐10个数据备份与恢复工具?先搞懂这3种备份方式,再选才不踩坑!
  • 手把手教你降AI不伤文:保姆级操作让论文既通过检测又保持专业
  • FPGA 实现多路高精度 AD1246 高速数据采集与接收设计
  • ACPI!gReadyQueue中的plistCtxtQ和ACPI!GetOpRegionScopeWorker函数中的赋值*state->PciObj = state->Parent