diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2023-05-12 09:15:59 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2023-05-12 09:15:59 +0200 |
| commit | ff68a95c6cac90d511290265b2e6c1dde1c0278a (patch) | |
| tree | 36f5f83fa0bcbebf62fb231132860d41f8b5cfc1 /coding-exercises/2/83/install-real.rkt | |
| parent | 2c8bbf5955c687b8e1528e8c637d3ae18960797f (diff) | |
finished chapter 2 finally
Diffstat (limited to 'coding-exercises/2/83/install-real.rkt')
| -rw-r--r-- | coding-exercises/2/83/install-real.rkt | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/coding-exercises/2/83/install-real.rkt b/coding-exercises/2/83/install-real.rkt index 2141059..2089437 100644 --- a/coding-exercises/2/83/install-real.rkt +++ b/coding-exercises/2/83/install-real.rkt @@ -19,12 +19,18 @@ ;; methods (define (gcd-real a b) - (if (and (integer? a) - (integer? b)) - (if (= b 0) - a - (gcd-real b (remainder a b))) - a)) + (cond ((and (integer? a) + (integer? b)) + (if (= b 0) + a + (gcd-real b (remainder a b)))) + ((= b 1) a) + (else (error "gcd for reals not implemented")))) + + (define (reduce-integers n d) + (let ((g (gcd-real n d))) + (list (/ n g) + (/ d g)))) (put 'add '(real real) (lambda (x y) (tagme (make (+ x y))))) (put 'neg '(real) (lambda (x) (tagme (make (- x))))) @@ -36,6 +42,7 @@ ((get 'make 'rational) n 1.0))) (put 'greatest-common-divisor '(real real) (lambda (a b) (tagme (gcd-real a b)))) ;; expt for integerizing factor + (put 'reduce '(real real) (lambda (n d) (reduce-integers n d))) (put 'expt '(real real) expt) ;; sqrt and trig methods for complex nums (put 'sqr '(real) sqr) |
