-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrationals.scm
92 lines (86 loc) · 1.97 KB
/
rationals.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(import (core/math.scm))
(define (makerat a b)
(cons a (cons b nil)))
(define (divisor r)
(car (cdr r)))
(define (numerator r)
(car r))
;(numerator (makerat 10 20))
(macro int_limit () 16384)
(define (bigger r)
(max (numerator r) (divisor r)))
;(bigger (makerat 4 5))
(define (simplify_more S R)
(makerat (/ (numerator R) S)
(/ (divisor R) S)))
;(simplify_more 1 (makerat 10 8))
;0
;(simplify_more 12 (makerat 100000 200000))
(define (simplify R)
(simplify_more
(max (/
(bigger R)
(int_limit))
1)
R))
;(simplify (makerat 100000 200000))
(define (add_rat a b)
(simplify
(makerat
(+ (* (numerator a)
(divisor b))
(* (numerator b)
(divisor a)))
(* (divisor a)
(divisor b)))))
;(max
; (/ (bigger (makerat 10 8)) (int_limit))
; 9)
;(simplify (makerat 10 8))
;(add_rat (makerat 1 2)
; (makerat 3 4))
(define (additive_inverse_rat r)
(makerat
(- 0 (numerator r))
(divisor r)))
;(additive_inverse_rat (makerat 5 6))))
(define (sub_rat a b)
(add_rat a (additive_inverse_rat b)))
;(sub_rat (makerat 5 4)) (makerat 1 10))))
(define (multiplicative_inverse r)
(makerat
(divisor r)
(numerator r)))
;(multiplicative_inverse (makerat 4 5))))
(define (mul_rat a b)
(simplify
(makerat
(* (numerator a)
(numerator b))
(* (divisor a)
(divisor b)))))
;(mul_rat (makerat 3 4)) (makerat 1 2))))
(define (div_rat a b)
(mul_rat a (multiplicative_inverse b)))
;(div_rat (makerat 1 2) (makerat 2 1)
(define (<rat a b)
(< (* (numerator a)
(divisor b))
(* (divisor a)
(numerator b))))
;(<rat (makerat 4 2)
; (makerat 3 4))
(define (>rat a b)
(not (<rat a b)))
(define (square_rat a)
(mul_rat a a))
;(mul_rat (makerat 1 4))
; (makerat 3 4))))
(define (pos_diff_rat a b)
(cond (((>rat a b) (sub_rat a b))
(true (sub_rat b a)))))
;(pos_diff_rat (makerat 1 2)
; (makerat 5 4))
;1
;4 4 4
;1