Last active
February 13, 2021 00:58
-
-
Save yanfeng42/8acfd4cec640f7ca976c053917f28656 to your computer and use it in GitHub Desktop.
sicp 1.22 的chez scheme 实现.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 基于 https://cisco.github.io/ChezScheme/csug9.5/csug9_5.pdf 的 12.10. Times and Dates | |
(define (time-prime-test n) | |
(newline) | |
(display n) | |
(start-prime-test n (current-time)) | |
) | |
(define (start-prime-test n start-time) | |
(if (prime? n) | |
(report-prime (time-difference (current-time) start-time)) | |
) | |
) | |
(define (report-prime elapsed-time) | |
(display " *** ") | |
; 输出的是纳秒. | |
(display (time-nanosecond elapsed-time)) | |
) | |
(define (search-for-primes n) | |
(search-for-primes-counter n 3) | |
) | |
(define (search-for-primes-counter n count) | |
(if (> count 0) | |
(if (even? n) | |
(search-for-primes-counter (+ n 1) count) | |
; 知识点: 平级子句, 用 begin 连接. | |
(if (prime? n) | |
(begin | |
(time-prime-test n) | |
(search-for-primes-counter (+ n 2) (- count 1)) | |
) | |
(search-for-primes-counter (+ n 2) count) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
相较 http://community.schemewiki.org/?sicp-ex-1.22 而言, 我的实现, 可以直接求得最终的结果,且不用修改 time-prime-test 的定义.