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

scheme中的辛普森积分

本章主要内容在于将基础计算的过程抽象出来成为一个普遍流程,以此增加函数应用的普遍性,提高后续使用时的上限。
练习1.29 辛普森积分
辛普森积分的数学性比较强,重点在于抽象积分过程,对积分系数进行分类讨论。

点击查看代码
(define (even? x)( = (remainder x 2) 0))
(define (cube x)( * x x x))
(define (sum term a next b)(if ( > a b)0(+ (term a)(sum term (next a) next b))))
(define (simpsons f a b n)(define h ( / (- b a) n))(define (y k) (f (+ a (* k h))))(define (factor k)(cond ((or (= k 0) ( = k n)) 1) ((even? k) 2)(else 4)))(define (term k) (* (factor k)(y k)))(define (next k) (+ k 1))(* ( / h 3) (sum term next n))
)

1.30 将抽象中的递归过程改写为迭代过程,练手题。

点击查看代码
(define (sum term a next b)(define (iter a result)(if ( > a b)result(iter (next a) ( + (term a) result))))(iter a 0))

1.31 对pi值的乘法展开过程的抽象

点击查看代码
(define (even? x)(= (remainder x 2.0) 0))
(define (next x) (+ x 2))
(define (f a b k n)(cond ((> k n) 1.0)((even? k) (* (/ a b) ( f (next a) b (+ k 1) n)))(else (* (/ a b) ( f a (next b) (+ k 1) n)))))
(define (f 2 3 0 100))

同1.30 将乘法过程做一个迭代的改写

点击查看代码
(define (product term a next b)(define (iter a result))(if ( > a b)result(iter (next a) (* (term a) result)))(iter a 1))

1.32 引入combiner和null-value,将乘法和加法过程统一抽象为一个过程。

点击查看代码
(define (accmulate combiner null-value term a next b)(if (> a b)null-value(combiner (term a)(accumulate combiner null-value (next a) next b))))

1.33中的一个实例,通过accmulate的过程结合一定的筛选方式(如prime筛选素数)进行特定范围的计算。

点击查看代码
(define (prime? n)( = n (smallest-divisor n)))
(define (smallest-divisor n)( find-divisor n 2))
(define (find-divisor n test-divisor)(cond ((> (* test-divisor test-divisor) (* n n)) n)((divisor? test-divisor n) test-divisor)(else (find-divisor n (+ test-divisor 1)))))
(define (divisor? a b)( = (remainder b a) 0))
(define (ori a) a)
(define (next a) (+ a 1))
(define (filtered-accmulate prime? combine null-value term a next b)(cond ((> a b) null-value)((prime? a) (combine (term a)  (filtered-accmulate prime? combine null-value term (next a) next b)))(else (filtered-accmulate prime? combine null-value term (next a) next b))))
(define (prime-sum a b)(filtered-accmulate prime? + 0 ori a next b))
http://www.jsqmd.com/news/59945/

相关文章:

  • 2025最新成都精装房装修公司推荐!家装实力品牌榜单发布,品质服务双优打造理想家居
  • 2025最新成都精装房装修公司推荐!蓉城家装实力品牌榜单发布,品质服务双优打造理想家居
  • 图书馆管理系统项目冲刺博客 Day3
  • 2025年下半年套管工厂综合推荐榜单:行业专家权威评测
  • 2025年11月热缩套管生产厂家哪家好深度评测
  • EmotiVoice 易魔声下载安装教程攻略:免费离线文本转语音工具软件新手必备
  • 2025年下半年热缩套管供应商靠谱排行Top 5推荐榜单
  • P4_准备西瓜语义分割数据集
  • 蓝桥杯Python-语法基础-1
  • 微信共享位置怎么修改虚拟位置
  • 图书馆管理系统项目冲刺博客 Day2
  • Java 创建事务的方式
  • 从“访答”出发,深入解析AI对话技术如何重塑信息获取方式
  • P3_安装配置MMSegmentation+预训练语义分割推理预测
  • 2025住人集装箱房生产品牌排名如何?谁在颠覆未来居住?
  • R20-2025年国产活动板房选购指南:领军品牌深度解析
  • langchain4j 学习系列(5)-RAG
  • [linux 交换空间]
  • Alpha冲刺总结报告
  • 20251203周三日记
  • 了解NFSv4中的nfsidmap
  • 你的学习思路有实践导向的优势,但需调整顺序和手段才能更高效成体系!核心结论:先搭建,边做边补原理,再集中突破面试考点,比 “先堆方案再回头学” 更高效。
  • Scrum 冲刺博客_5
  • 2025年11月聚乙烯瓶厂家综合评估与选购指南:十大知名供应商深度解析
  • 2025年12月广东顺德短视频代运营团队优势解读
  • 为什么需要多路召回
  • 2025年11月聚乙烯瓶厂家综合排行榜:权威推荐与选购指南
  • 2025年11月农药瓶供应商排行榜:安徽金汇龙包装位居榜首
  • 2025年11月农药瓶供应商排行榜:安徽金汇龙包装领跑行业
  • (论文阅读)An Image is Worth 32 Tokens for Reconstruction and Generation