summaryrefslogtreecommitdiff
path: root/coding-exercises
diff options
context:
space:
mode:
authorMike Vink <mike1994vink@gmail.com>2023-05-05 17:41:26 +0200
committerMike Vink <mike1994vink@gmail.com>2023-05-05 17:41:26 +0200
commit5bccd56659ea72a6f85d71503f7cc4512bc45950 (patch)
tree869cb206eef43b720204f2ae8e5f57e6ceb96723 /coding-exercises
parenta3c770f64ce5ee5e2ee3ca2c41a94c111d35c3c2 (diff)
finishing up 87
Diffstat (limited to 'coding-exercises')
-rw-r--r--coding-exercises/2/83/install-integer.rkt3
-rw-r--r--coding-exercises/2/83/install-rational.rkt2
-rw-r--r--coding-exercises/2/87.rkt42
3 files changed, 45 insertions, 2 deletions
diff --git a/coding-exercises/2/83/install-integer.rkt b/coding-exercises/2/83/install-integer.rkt
index 2fabf3a..ca1ebb5 100644
--- a/coding-exercises/2/83/install-integer.rkt
+++ b/coding-exercises/2/83/install-integer.rkt
@@ -16,6 +16,7 @@
(put 'make 'integer (lambda (x) (tagme (make x))))
;; methods
(put 'add '(integer integer) (lambda (x y) (tagme (make (+ x y)))))
+ (put 'neg '(integer) (lambda (x) (tagme (- x))))
(put 'sub '(integer integer) (lambda (x y) (tagme (make (- x y)))))
(put 'mul '(integer integer) (lambda (x y) (tagme (make (* x y)))))
(put 'div '(integer integer) (lambda (x y) (tagme (make (/ x y)))))
@@ -23,7 +24,7 @@
;; sqrt and trig methods for complex nums
(put 'sqr '(integer) sqr)
(put 'sqrt '(integer) sqrt)
- (put 'atan '(integer) atan)
+ (put 'atan '(integer integer) atan)
(put 'cos '(integer) cos)
(put 'sin '(integer) sin)
;; predicates
diff --git a/coding-exercises/2/83/install-rational.rkt b/coding-exercises/2/83/install-rational.rkt
index 96dcef1..73640b7 100644
--- a/coding-exercises/2/83/install-rational.rkt
+++ b/coding-exercises/2/83/install-rational.rkt
@@ -46,6 +46,8 @@
;; interface
(put 'add '(rational rational)
(lambda (x y) (tagme (add-rat x y))))
+ (put 'neg '(rational)
+ (lambda (rat) (tagme (neg-rat rat))))
(put 'sub '(rational rational)
(lambda (x y) (tagme (sub-rat x y))))
(put 'mul '(rational rational)
diff --git a/coding-exercises/2/87.rkt b/coding-exercises/2/87.rkt
index d73093e..f368ed4 100644
--- a/coding-exercises/2/87.rkt
+++ b/coding-exercises/2/87.rkt
@@ -64,6 +64,17 @@
(term-list p2)))
(error "Polys not in same var -- ADD-POLY" (list p1 p2))))
+ (define (neg-poly poly)
+ (make-poly
+ (variable poly)
+ (map (lambda (term)
+ (make-term
+ (order term)
+ (apply-fn 'neg (coeff term))))
+ (term-list poly))))
+ (define (sub-poly p1 p2)
+ (add-poly p1 (neg-poly p2)))
+
(define (mul-term-by-all-terms t1 L)
(if (empty-termlist? L)
(the-empty-termlist)
@@ -84,8 +95,17 @@
(mul-terms (term-list p1)
(term-list p2)))
(error "Polys not in same var -- MUL-POLY" (list p1 p2))))
+ (define (polynomial-=zero? poly)
+ (define (rec term-list)
+ (cond ((empty-termlist? term-list) true)
+ ((not (apply-fn '=zero? (coeff (first-term term-list)))) false)
+ (else (rec (rest-terms term-list)))))
+ (rec (term-list poly)))
+ (put '=zero? '(polynomial) polynomial-=zero?)
;;interface
(put 'add '(polynomial polynomial) (lambda (p1 p2) (tagme (add-poly p1 p2))))
+ (put 'neg '(polynomial) (lambda (p) (tagme (neg-poly p))))
+ (put 'sub '(polynomial polynomial) (lambda (p1 p2) (tagme (sub-poly p1 p2))))
(put 'mul '(polynomial polynomial) (lambda (p1 p2) (tagme (mul-poly p1 p2))))
(put 'make 'polynomial
(lambda (var terms) (tagme (make-poly var terms))))
@@ -101,6 +121,26 @@
(list 3 test-real)
(list 1 test-rat)
(list 0 test-integer))))
+(define test-poly3 (make-polynomial 'x (list
+ (list 1 2)
+ (list 0 2))))
+((lambda ()
+ (newline)
+ (display (add test-poly2 test-poly2))
+ (newline)
+ (display (mul test-poly2 test-poly2))
+ (newline)
+ (display (mul test-poly3 test-poly3))))
+
+;;87
+(=zero? test-poly3)
+(=zero? (make-polynomial 'x (list (list 1000 0))))
+
+;; 88
+;; what is meant with negation here? Negation of a number? Making a negative number?
+;; Guess that would be handy if we need to subtract a lot of terms.
((lambda ()
(newline)
- (display (add test-poly2 test-poly2))))
+ (display (sub test-poly1 test-poly3))
+ (newline)
+ (display (sub test-poly1 test-poly2))))