Skip to content

Commit 0f6b4e1

Browse files
committed
Add implementation of exercise 3-74
1 parent 2efbaac commit 0f6b4e1

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

README.org

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
习题完成情况:
2626
- 章节一: 43/46
2727
- 章节二: 88/97
28-
- 章节三: 67/82
28+
- 章节三: 68/82
2929
- 章节四: TODO
3030
- 章节五: TODO
3131
* 运行
@@ -144,18 +144,22 @@
144144
#+end_src
145145

146146
这里的蒙特卡罗实现真的是优雅
147-
*** 翻译错误
148-
第2版,238页,模块化,对象和状态,中文翻译是
149-
#+begin_quote
150-
练习3.70
151-
b) 所有正正数序对 (i,j) 的流,其中要求 i<=j, 而且这里的i或者j可以被2,3或者5整除
152-
#+end_quote
153-
154-
原文是:
155-
#+begin_quote
156-
the stream of all pairs of positive integers ( i , j ) with i ≤ j , where neither i nor j is divisible by 2, 3, or 5,
157-
#+end_quote
158-
159-
https://sarabander.github.io/sicp/html/3_002e5.xhtml#g_t3_002e5
147+
** 翻译错误
148+
第2版,238页,模块化,对象和状态,中文翻译是
149+
#+begin_quote
150+
练习3.70
151+
b) 所有正正数序对 (i,j) 的流,其中要求 i<=j, 而且这里的i或者j可以被2,3或者5整除
152+
#+end_quote
153+
154+
原文是:
155+
#+begin_quote
156+
the stream of all pairs of positive integers ( i , j ) with i ≤ j , where neither i nor j is divisible by 2, 3, or 5,
157+
#+end_quote
158+
159+
https://sarabander.github.io/sicp/html/3_002e5.xhtml#g_t3_002e5
160160

161-
i和j不可以被2,3,5整除,翻译成了可以被2,3或者5整除,意思完全相反,真的是服了。
161+
i和j不可以被2,3,5整除,翻译成了可以被2,3或者5整除,意思完全相反,真的是服了。
162+
** 排版错误
163+
第2版,240页,模块化,对象和状态
164+
165+
练习3.74 输入信号流和过零点信号流完全没有对齐

chapter3/exercise3-74.rkt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#lang racket
2+
(require "stream.rkt")
3+
(require "exercise3-50.rkt")
4+
5+
(define (sign-change-detector current prev)
6+
(cond ((and (< prev 0) (>= current 0)) 1)
7+
((and (>= prev 0) (< current 0)) -1)
8+
(else 0)))
9+
10+
(define (make-zero-crossings input-stream last-value)
11+
(if (stream-null? input-stream)
12+
the-empty-stream
13+
(cons-stream
14+
(sign-change-detector (stream-car input-stream) last-value)
15+
(make-zero-crossings (stream-cdr input-stream)
16+
(stream-car input-stream)))))
17+
18+
(provide sign-change-detector make-zero-crossings)
19+
(define sense-data (list-to-stream '(1 2 1.5 1 0.5 -0.1 -2 -3 -2 -0.5 0.2 3 4)))
20+
21+
(define zero-crossings (map-stream sign-change-detector (stream-cdr sense-data) sense-data))
22+
23+
(module+ test
24+
(require rackunit)
25+
26+
(test-case "Test for make-zero-crossings"
27+
(define zero-crossings (make-zero-crossings sense-data 0))
28+
(check-equal? (stream-take-n zero-crossings 13) '(0 0 0 0 0 -1 0 0 0 0 1 0 0))
29+
)
30+
(test-case "Test for zero-crossings"
31+
(check-equal? (stream-take-n zero-crossings 11) '(0 0 0 0 -1 0 0 0 0 1 0))
32+
)
33+
)

0 commit comments

Comments
 (0)