【SICP练习】125 练习3.56
2015-09-08 00:00
337 查看
练习3-56
原文
Exercise 3.56. A famous problem, first raised by R. Hamming, is to enumerate, in ascending order with no repetitions, all positive integers with no prime factors other than 2, 3, or 5. One obvious way to do this is to simply test each integer in turn to see whether it has any factors other than 2, 3, and 5. But this is very inefficient, since, as the integers get larger, fewer and fewer of them fit the requirement. As an alternative, let us call the required stream of numbers S and notice the following facts about it.● S begins with 1.
● The elements of (scale-stream S 2) are also elements of S.
● The same is true for (scale-stream S 3) and (scale-stream 5 S).
● These are all the elements of S.
Now all we have to do is combine elements from these sources. For this we define a procedure merge that combines two ordered streams into one ordered result stream, eliminating repetitions:
(define (merge s1 s2) (cond ((stream-null? s1) s2) ((stream-null? s2) s1) (else (let ((s1car (stream-car s1)) (s2car (stream-car s2))) (cond ((< s1car s2car) (cons-stream s1car (merge (stream-cdr s1) s2))) ((> s1car s2car) (cons-stream s2car (merge s1 (stream-cdr s2)))) (else (cons-stream s1car (merge (stream-cdr s1) (stream-cdr s2)))))))))
Then the required stream may be constructed with merge, as follows:
(define S (cons-stream 1 (merge <??> <??>)))
Fill in the missing expressions in the places marked
代码
(define s (cons-stream 1 (merge (scale-stream s 2) (merge (scale-stream s 3) (scale-stream s 5)))))
感谢访问,希望对您有所帮助。 欢迎关注或收藏、评论或点赞。
为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp
版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp
相关文章推荐
- Java IO与NIO的一些文件拷贝测试
- Ruby中使用Block、Proc、lambda实现闭包
- Ruby中的block、proc、lambda区别总结
- Stream、WshShell、WshUrlShortcut对象及Shell.Application的参数与使用
- C++实现的一个可以写递归lambda的Y函数
- C#特性之匿名方法和Lambda表达式
- Node.js中的流(Stream)介绍
- php中stream(流)的用法
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- 理解C#中的Lambda表达式
- 初步认识C#中的Lambda表达式和匿名方法
- C#3.0中Lambda表达式详解
- C#基础之Lambda表达式用法实例教程
- C# Stream 和 byte[] 之间的转换
- Python enumerate遍历数组示例应用
- 利用stream实现一个简单的http下载器
- 浅谈PHP中Stream(流)
- php常用Stream函数集介绍
- 深入理解Java中的Lambda表达式
- Java8新特性之Lambda表达式浅析