Skip to content

Commit e463c62

Browse files
committed
add polynomial number system(WIP).
1 parent 6e44c22 commit e463c62

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

chapter2/poly.rkt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#lang racket
2+
(require "generic_operation.scm")
3+
4+
(define (variable? x) (symbol? x))
5+
6+
(define (same-variable? v1 v2)
7+
(and (variable? v1) (variable? v2) (eq? v1 v2)))
8+
9+
(define (install-polynomial-package)
10+
;; internal procedures
11+
12+
;; representation of poly
13+
(define (make-poly variable term-list)
14+
(cons variable term-list))
15+
16+
(define (variable p) (car p))
17+
18+
(define (term-list p) (cdr p))
19+
20+
(define (add-poly p1 p2)
21+
(if (same-variable? (variable p1) (variable p2))
22+
(make-poly (variable p1)
23+
(add-terms (term-list p1)
24+
(term-list p2)))
25+
(error "Polys not in same var -- ADD-POLY" (list p1 p2))))
26+
27+
(define (mul-poly p1 p2)
28+
(if (same-variable? (variable p1) (variable p2))
29+
(make-poly (variable p1)
30+
(mul-terms (term-list p1)
31+
(term-list p2)))
32+
(error "Polys not in same var -- MUL-POLY" (list p1 p2))))
33+
34+
;; interface to the rest of the system.
35+
(define (tag p) (attach-tag 'polynomial p))
36+
37+
(put 'add '(polynomial polynomial) (lambda (p1 p2) (tag (add-poly p1 p2))))
38+
39+
(put 'mul '(polynomial polynomial) (lambda (p1 p2) (tag (mul-poly p1 p2))))
40+
41+
(put 'make 'polynomial (lambda (var terms) (tag (make-poly var terms))))
42+
43+
'done)

0 commit comments

Comments
 (0)