Skip to content

Commit 94bd219

Browse files
committed
Add implementation of exercise 3-75
1 parent 0f6b4e1 commit 94bd219

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
习题完成情况:
2626
- 章节一: 43/46
2727
- 章节二: 88/97
28-
- 章节三: 68/82
28+
- 章节三: 69/82
2929
- 章节四: TODO
3030
- 章节五: TODO
3131
* 运行

chapter3/exercise3-75.rkt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#lang racket
2+
(require "stream.rkt")
3+
(require "exercise3-74.rkt")
4+
5+
(define (make-zero-crossings
6+
input-stream last-value)
7+
(let ((avpt
8+
(/ (+ (stream-car input-stream)
9+
last-value)
10+
2)))
11+
(cons-stream
12+
(sign-change-detector avpt last-value)
13+
(make-zero-crossings
14+
;; last-value 参数传入的是平均值,而非前一个值
15+
(stream-cdr input-stream) avpt))))
16+
17+
(define (make-zero-crossings-smooth input-stream last-value last-avg)
18+
(if (stream-null? input-stream)
19+
the-empty-stream
20+
(let ((avpt
21+
(/ (+ (stream-car input-stream)
22+
last-value)
23+
2)))
24+
(cons-stream
25+
(sign-change-detector avpt last-avg)
26+
(make-zero-crossings-smooth
27+
(stream-cdr input-stream)
28+
(stream-car input-stream)
29+
avpt)))))
30+
31+
(module+ test
32+
(require rackunit)
33+
34+
(test-case "Test for make-zero-crossings-smooth"
35+
(define sense-data (list-to-stream '(10 2 -2 -10 10 -1)))
36+
(define crossings (make-zero-crossings-smooth sense-data 0 0))
37+
(check-equal? (stream-take-n crossings 6) '(0 0 0 -1 1 0)))
38+
)

0 commit comments

Comments
 (0)