当前位置: 首页 > 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.gsyq.cn/news/70444.html

相关文章:

  • sql文件无法设置打开方式为ssms19,重新安装也不行
  • 2025年下半年套管工厂综合推荐榜单:行业专家权威评测
  • 蓝桥杯Python-语法基础-1
  • 图书馆管理系统项目冲刺博客 Day2
  • 2025住人集装箱房生产品牌排名如何?谁在颠覆未来居住?
  • 完整教程:【高阶数据结构】AVL树
  • 了解NFSv4中的nfsidmap
  • 你的学习思路有实践导向的优势,但需调整顺序和手段才能更高效成体系!核心结论:先搭建,边做边补原理,再集中突破面试考点,比 “先堆方案再回头学” 更高效。
  • 2025年11月聚乙烯瓶厂家综合评估与选购指南:十大知名供应商深度解析
  • 2025年11月农药瓶供应商排行榜:安徽金汇龙包装位居榜首
  • (论文阅读)An Image is Worth 32 Tokens for Reconstruction and Generation
  • 酷我音乐APP(手机音乐播放器) v12.0.0.2 去广告破解版
  • AI元人文:没有AI辅助,价值维度谱何以可能?
  • 2025年12月美国移民,马耳他移民,香港移民机构推荐榜,彰显合规服务与全球资源实力
  • 2025年12月澳洲移民,美国移民,新加坡移民机构推荐:聚焦资质案例与成功率深度解析
  • 颜色空间,线性和伽马颜色空间
  • 2025年12月葡萄牙移民,新加坡移民,香港移民机构推荐榜:甄选合规中介实测解析
  • 2025年7款免费AI论文写作工具推荐:毕业论文一键生成神器
  • 第7篇 Scrum 冲刺博客
  • 关于Java JSON库的选择
  • 补发读后感2
  • qy_蓝桥杯编程系列_编程18 进制转换
  • 递归函数,闭包,装饰器3
  • How to do a biology experiment for a Physician.
  • LC 3479(2100) 线段树二分 水果成篮
  • 2025年上海办公室装修公司口碑排名:迎湖办公室装修实力可靠
  • Scrum 冲刺博客_4
  • 第二天项目
  • 2025年西南五大诚信的加拿大移民企业推荐,看哪家售后服务优
  • 2025年中国仿石砖十大龙头厂家推荐:看哪家产品质量好?