Complete up to 2.86
We now have complex numbers with arbitrary number coefficients (ie integer, rational or real).
This commit is contained in:
parent
d391dded38
commit
177de641b5
68
2_78.rkt
68
2_78.rkt
|
@ -1,9 +1,6 @@
|
|||
#lang sicp
|
||||
|
||||
|
||||
;;;from chapter 1
|
||||
(define (square x) (* x x))
|
||||
|
||||
;;;from section 1.2.5, for Section 2.1.1
|
||||
(define (gcd a b)
|
||||
(if (= b 0)
|
||||
|
@ -153,15 +150,28 @@
|
|||
(define (install-integer-package)
|
||||
(define (tag x)
|
||||
(attach-tag 'integer x))
|
||||
(define (raise-int x)
|
||||
(make-rational x 1))
|
||||
|
||||
(put 'add '(integer integer)
|
||||
(lambda (x y)
|
||||
(make-integer (+ x y))))
|
||||
(put 'mul '(integer integer)
|
||||
(lambda (x y)
|
||||
(make-integer (* x y))))
|
||||
(put 'sine '(integer)
|
||||
(lambda (x) (sine (raise-int x))))
|
||||
(put 'cosine '(integer)
|
||||
(lambda (x) (cosine (raise-int x))))
|
||||
(put 'square-root '(integer)
|
||||
(lambda (x) (square-root (raise-int x))))
|
||||
(put 'arctan '(integer integer)
|
||||
(lambda (x y) (arctan (raise-int x)
|
||||
(raise-int y))))
|
||||
(put 'equ? '(integer integer)
|
||||
(lambda (x y)
|
||||
(= x y)))
|
||||
(put 'raise '(integer)
|
||||
(lambda (x) (make-rational x 1)))
|
||||
(put 'raise '(integer) raise-int)
|
||||
(put 'project '(integer)
|
||||
(lambda (x) '()))
|
||||
(put 'make 'integer
|
||||
|
@ -177,6 +187,17 @@
|
|||
(put 'add '(real real)
|
||||
(lambda (x y)
|
||||
(make-real (+ x y))))
|
||||
(put 'mul '(real real)
|
||||
(lambda (x y)
|
||||
(make-real (* x y))))
|
||||
(put 'sine '(real)
|
||||
(lambda (x) (make-real (sin x))))
|
||||
(put 'cosine '(real)
|
||||
(lambda (x) (make-real (cos x))))
|
||||
(put 'square-root '(real)
|
||||
(lambda (x) (make-real (sqrt x))))
|
||||
(put 'arctan '(real real)
|
||||
(lambda (x y) (make-real (atan x y))))
|
||||
(put 'equ? '(real real)
|
||||
(lambda (x y)
|
||||
(= x y)))
|
||||
|
@ -216,6 +237,8 @@
|
|||
(define (eq-rat x y)
|
||||
(and (= (numer x) (numer y))
|
||||
(= (denom x) (denom y))))
|
||||
(define (raise-rat x)
|
||||
(make-real (/ (numer x) (denom x))))
|
||||
;; interface to rest of the system
|
||||
(define (tag x) (attach-tag 'rational x))
|
||||
(put 'add '(rational rational)
|
||||
|
@ -228,12 +251,19 @@
|
|||
(lambda (x y) (tag (mul-rat x y))))
|
||||
(put 'div '(rational rational)
|
||||
(lambda (x y) (tag (div-rat x y))))
|
||||
(put 'sine '(rational)
|
||||
(lambda (x) (sine (raise-rat x))))
|
||||
(put 'cosine '(rational)
|
||||
(lambda (x) (cosine (raise-rat x))))
|
||||
(put 'square-root '(rational)
|
||||
(lambda (x) (square-root (raise-rat x))))
|
||||
(put 'arctan '(rational rational)
|
||||
(lambda (x y) (arctan (raise-rat x)
|
||||
(raise-rat y))))
|
||||
(put 'equ? '(rational rational) eq-rat)
|
||||
(put '=zero? '(rational)
|
||||
(lambda (x) (= (numer x) 0)))
|
||||
(put 'raise '(rational)
|
||||
(lambda (x)
|
||||
(make-real (/ (numer x) (denom x)))))
|
||||
(put 'raise '(rational) raise-rat)
|
||||
(put 'project '(rational)
|
||||
(lambda (x)
|
||||
(make-integer
|
||||
|
@ -254,12 +284,13 @@
|
|||
(define (imag-part z) (cdr z))
|
||||
(define (make-from-real-imag x y) (cons x y))
|
||||
(define (magnitude z)
|
||||
(sqrt (+ (square (real-part z))
|
||||
(square (imag-part z)))))
|
||||
(square-root (add (square (real-part z))
|
||||
(square (imag-part z)))))
|
||||
(define (angle z)
|
||||
(atan (imag-part z) (real-part z)))
|
||||
(arctan (imag-part z) (real-part z)))
|
||||
(define (make-from-mag-ang r a)
|
||||
(cons (* r (cos a)) (* r (sin a))))
|
||||
(cons (mul r (cosine a))
|
||||
(mul r (sine a))))
|
||||
|
||||
;; interface to the rest of the system
|
||||
(define (tag x) (attach-tag 'rectangular x))
|
||||
|
@ -279,12 +310,12 @@
|
|||
(define (angle z) (cdr z))
|
||||
(define (make-from-mag-ang r a) (cons r a))
|
||||
(define (real-part z)
|
||||
(* (magnitude z) (cos (angle z))))
|
||||
(mul (magnitude z) (cosine (angle z))))
|
||||
(define (imag-part z)
|
||||
(* (magnitude z) (sin (angle z))))
|
||||
(mul (magnitude z) (sine (angle z))))
|
||||
(define (make-from-real-imag x y)
|
||||
(cons (sqrt (+ (square x) (square y)))
|
||||
(atan y x)))
|
||||
(cons (square-root (add (square x) (square y)))
|
||||
(arctan y x)))
|
||||
|
||||
;; interface to the rest of the system
|
||||
(define (tag x) (attach-tag 'polar x))
|
||||
|
@ -386,6 +417,11 @@
|
|||
(define (sub x y) (apply-generic 'sub x y))
|
||||
(define (mul x y) (apply-generic 'mul x y))
|
||||
(define (div x y) (apply-generic 'div x y))
|
||||
(define (sine x) (apply-generic 'sine x))
|
||||
(define (cosine x) (apply-generic 'cosine x))
|
||||
(define (square-root x) (apply-generic 'square-root x))
|
||||
(define (arctan x y) (apply-generic 'arctan x y))
|
||||
(define (square x) (mul x x))
|
||||
|
||||
(define (equ? x y) (apply-generic 'equ? x y))
|
||||
;;(put 'equ? '(complex complex) equ?)
|
||||
|
|
Loading…
Reference in New Issue