-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex2-01-better-rational-math.scm
58 lines (45 loc) · 1.62 KB
/
ex2-01-better-rational-math.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
; Ex. 2.1 A better implementation of rational arithmetic
; Construct rational number as a pair object.
; ; This improves upon the make-rat procedure of Section 2.1.2.
(define (make-rat n d)
(let ((norm-n (abs (/ n (gcd n d))))
(norm-d (abs (/ d (gcd n d))))
(same-sign? (or (and (< n 0) (< d 0))
(and (> n 0) (> d 0)))))
(cond ((= n 0)
(cons 0 1))
((= d 0)
(error "The denominator must not be zero: " d))
(same-sign?
(cons norm-n norm-d))
(else
(cons (- norm-n) norm-d)))))
; Extract the numerator and/or denominator of a rational number,
; assuming it is represented as a pair object.
(define (numer x) (car x))
(define (denom x) (cdr x))
; Perform rational number arithmetic operations
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (numer x) (numer y))))
(define (div-rat x y)
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
(define (equal-rat? x y)
(= (* (numer x) (denom y))
(* (numer y) (denom x))))
; Display a pair object using the conventional rational number
; notation.
(define (print-rat x)
(newline)
(display (numer x))
(display "/")
(display (denom x)))