summaryrefslogtreecommitdiff
path: root/coding-exercises/2/83/install-real.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'coding-exercises/2/83/install-real.rkt')
-rw-r--r--coding-exercises/2/83/install-real.rkt10
1 files changed, 10 insertions, 0 deletions
diff --git a/coding-exercises/2/83/install-real.rkt b/coding-exercises/2/83/install-real.rkt
index d82d48a..a232e5e 100644
--- a/coding-exercises/2/83/install-real.rkt
+++ b/coding-exercises/2/83/install-real.rkt
@@ -17,6 +17,15 @@
;; constructor
(put 'make 'real (lambda (x) (tagme (make x))))
;; methods
+
+ (define (gcd-real a b)
+ (if (and (integer? a)
+ (integer? b))
+ (if (= b 0)
+ a
+ (gcd-real b (remainder a b)))
+ a))
+
(put 'add '(real real) (lambda (x y) (tagme (make (+ x y)))))
(put 'neg '(real) (lambda (x) (tagme (make (- x)))))
(put 'sub '(real real) (lambda (x y) (tagme (make (- x y)))))
@@ -25,6 +34,7 @@
(put 'raise '(real) raiseme)
(put 'project '(real) (lambda (n)
((get 'make 'rational) n 1.0)))
+ (put 'greatest-common-divisor '(real real) (lambda (a b) (tagme (gcd-real a b))))
;; sqrt and trig methods for complex nums
(put 'sqr '(real) sqr)
(put 'sqrt '(real) sqrt)