练习3-64
原文
Exercise 3.64. Write a procedure stream-limit that takes as arguments a stream and a number (the tolerance). It should examine the stream until it finds two successive elements that differ in absolute value by less than the tolerance, and return the second of the two elements. Using this, we could compute square roots up to a given tolerance by
(define (sqrt x tolerance) (stream-limit (sqrt-stream x) tolerance))
代码
(define (stream-limit stream tolerance) (if (< (abs (- (stream-ref stream 1) (stream-ref stream 0))) tolerance) (stream-ref stream 1) (stream-limit (stream-cdr stream) tolerance)))
感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.youkuaiyun.com/nomasp
版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.youkuaiyun.com/nomasp
本文深入探讨了流极限(stream-limit)算法的设计与实现,这是一种用于在数值流中寻找两个连续元素,其绝对值之差小于给定容差的技术。通过递归函数定义,展示了如何使用此算法来计算平方根,直至达到指定的精度。代码示例清晰地解释了算法的工作原理。
949

被折叠的 条评论
为什么被折叠?



