Skip to content

Commit d1deed3

Browse files
committed
Add implementation of exercise 3-58
1 parent 541bc60 commit d1deed3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-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-
- 章节三: 57/82
28+
- 章节三: 58/82
2929
- 章节四: TODO
3030
- 章节五: TODO
3131
* 运行

chapter3/exercise3-58.rkt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#lang racket
2+
(require "stream.rkt")
3+
4+
;;; 实际上实现了一个流式长除法算法, 用于将分数num/den转换为以radix为基数的无限小数表示
5+
(define (expand num den radix)
6+
(cons-stream
7+
(quotient (* num radix) den)
8+
(expand (remainder (* num radix) den) den radix)))
9+
10+
(module+ test
11+
(require rackunit)
12+
13+
(test-case "Test for expand"
14+
(define x (expand 3 8 10))
15+
;; 3/8 = 0.375 (有限小数)
16+
(check-equal? (stream-to-list x 10) '(3 7 5 0 0 0 0 0 0 0))
17+
18+
(define s (expand 1 7 10))
19+
;; (expand 1 7 10) 对应 1/7 = 0.142857142857... (循环小数)
20+
(check-equal? (stream-to-list s 10) '(1 4 2 8 5 7 1 4 2 8))))

0 commit comments

Comments
 (0)