Skip to content

Instantly share code, notes, and snippets.

@yanfeng42
Last active February 13, 2021 00:58
Show Gist options
  • Save yanfeng42/8acfd4cec640f7ca976c053917f28656 to your computer and use it in GitHub Desktop.
Save yanfeng42/8acfd4cec640f7ca976c053917f28656 to your computer and use it in GitHub Desktop.
sicp 1.22 的chez scheme 实现.
;; 基于 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)
)
)
)
)
@yanfeng42
Copy link
Author

相较 http://community.schemewiki.org/?sicp-ex-1.22 而言, 我的实现, 可以直接求得最终的结果,且不用修改 time-prime-test 的定义.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment