-
Notifications
You must be signed in to change notification settings - Fork 5
/
rationalize-expression.sls
60 lines (37 loc) · 1.29 KB
/
rationalize-expression.sls
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
#!r6rs
(library (mpl rationalize-expression)
(export rationalize-expression)
(import (mpl rnrs-sans)
(mpl misc)
(mpl arithmetic)
(mpl numerator)
(mpl denominator))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (rationalize-sum u v)
(let ((m (numerator u))
(r (denominator u))
(n (numerator v))
(s (denominator v)))
(if (and (equal? r 1)
(equal? s 1))
(+ u v)
(/ (rationalize-sum (* m s)
(* n r))
(* r s)))))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (rationalize-expression u)
(cond ( (power? u)
(^ (rationalize-expression (list-ref u 1))
(list-ref u 2)) )
( (product? u)
(let ((f (list-ref u 1)))
(* (rationalize-expression f)
(rationalize-expression (/ u f)))) )
( (sum? u)
(let ((f (list-ref u 1)))
(let ((g (rationalize-expression f))
(r (rationalize-expression (- u f))))
(rationalize-sum g r))) )
( else u )))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
)