Commit 2823a41f authored by bbguimaraes's avatar bbguimaraes
Browse files

sicp: section 2.1

parent d0e97325
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (equal-rat? x y)
(= (* (numer x) (denom y))
(* (numer y) (denom x))))
(define x (cons 1 2))
(display (car x))
(newline)
(display (cdr x))
(newline)
(define x (cons 1 2))
(define y (cons 3 4))
(define z (cons x y))
(display (car (car z)))
(newline)
(display (car (cdr z)))
(newline)
(define (make-rat n d) (cons n d))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (print-rat x)
(display (numer x))
(display "/")
(display (denom x)))
(define one-half (make-rat 1 2))
(print-rat one-half)
(newline)
(define one-third (make-rat 1 3))
(print-rat (add-rat one-half one-third))
(newline)
(print-rat (mul-rat one-half one-third))
(newline)
(print-rat (add-rat one-third one-third))
(newline)
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(print-rat (add-rat one-third one-third))
(newline)
(define (make-rat n d)
(cons n d))
(define (numer x)
(let ((g (gcd (car x) (cdr x))))
(/ (car x) g)))
(define (denom x)
(let ((g (gcd (car x) (cdr x))))
(/ (cdr x) g)))
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(define (print-rat x)
(display (numer x))
(display "/")
(display (denom x)))
(define one-half (make-rat 1 2))
(print-rat one-half)
(newline)
(define one-third (make-rat 1 3))
(print-rat (add-rat one-half one-third))
(newline)
(print-rat (mul-rat one-half one-third))
(newline)
(print-rat (add-rat one-third one-third))
(newline)
(define (cons x y)
(define (dispatch m)
(cond ((= m 0) x)
((= m 1) y)
(else (error "Argument not 0 or 1 -- CONS" m))))
dispatch)
(define (car z) (z 0))
(define (cdr z) (z 1))
(define c (cons 1 2))
(display (car c))
(newline)
(display (cdr c))
(newline)
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (div-interval x y)
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (print-rat x)
(display (numer x))
(display "/")
(display (denom x)))
(define (make-rat n d)
(define (make n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(if (> d 0)
(make n d)
(make (- n) (- d))))
(print-rat (make-rat 1 2))
(newline)
(print-rat (make-rat -1 -2))
(newline)
(print-rat (make-rat -1 2))
(newline)
(print-rat (make-rat 1 -2))
(newline)
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (div-interval x y)
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
(define (make-interval a b) (cons a b))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(define (print-interval i)
(display "(")
(display (lower-bound i))
(display ",")
(display (upper-bound i))
(display ")"))
(define i1 (make-interval 6.12 7.48))
(print-interval (div-interval i1 (make-interval 0 0)))
(newline)
(define (interval-width i)
(- (upper-bound i) (lower-bound i)))
(define (div-interval x y)
(if (= (interval-width y) 0) (error "Dividing by zero-width interval"))
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
(print-interval (div-interval i1 (make-interval 0 0)))
(newline)
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (make-interval a b) (if (<= a b) (cons a b) (make-interval b a)))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(define (print-interval i)
(display "(")
(display (lower-bound i))
(display ",")
(display (upper-bound i))
(display ")"))
(define i1 (make-interval 6.12 7.48))
(define i2 (make-interval 4.46 4.93))
(print-interval (mul-interval i1 i2))
(newline)
(define (mul-interval x y)
(define (p? x) (>= x 0))
(define (n? x) (< x 0))
(define (ret x0 x1 y0 y1) (make-interval (* x0 x1) (* y0 y1)))
(let ((x0 (lower-bound x))
(x1 (upper-bound x))
(y0 (lower-bound y))
(y1 (upper-bound y)))
(cond
((p? x0)
(cond
((p? y0) (ret x0 y0 x1 y1))
((p? y1) (ret x1 y0 x1 y1))
(else (ret x1 y0 x0 y1))))
((p? x1)
(cond
((p? y0) (ret x0 y1 x1 y1))
((p? y1) (make-interval (min (* x0 y1) (* x1 y0))
(max (* x0 y0) (* x1 y1))))
(else (ret x1 y0 x0 y0))))
(else
(cond
((p? y0) (ret x0 y1 x1 y0))
((p? y1) (ret x0 y1 x0 y0))
(else (ret x1 x1 x0 y0)))))))
(print-interval (mul-interval i1 i2))
(newline)
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (div-interval x y)
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
(define (make-interval a b) (cons a b))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(define (print-interval i)
(display "(")
(display (lower-bound i))
(display ",")
(display (upper-bound i))
(display ")"))
(define (sub-interval x y)
(make-interval (- (lower-bound x) (lower-bound y))
(- (upper-bound x) (upper-bound y))))
(define i1 (make-interval 6.12 7.48))
(define i2 (make-interval 4.465 4.935))
(print-interval (add-interval i1 i2))
(newline)
(print-interval (sub-interval i1 i2))
(newline)
(print-interval (mul-interval i1 i2))
(newline)
(print-interval (div-interval i1 i2))
(newline)
(define (make-center-width c w)
(make-interval (- c w) (+ c w)))
(define (center i)
(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)
(/ (- (upper-bound i) (lower-bound i)) 2))
(define i1 (make-center-width 6.8 0.68))
(define i2 (make-center-width 4.7 0.235))
(newline)
(print-interval (add-interval i1 i2))
(newline)
(print-interval (sub-interval i1 i2))
(newline)
(print-interval (mul-interval i1 i2))
(newline)
(print-interval (div-interval i1 i2))
(newline)
(define (make-center-percent c p)
(make-interval (* c (- 1 p)) (* c (+ 1 p))))
(define (percent i)
(/ (width i) (center i)))
(define i1 (make-center-percent 6.8 0.1))
(define i2 (make-center-percent 4.7 0.05))
(newline)
(print-interval (add-interval i1 i2))
(newline)
(print-interval (sub-interval i1 i2))
(newline)
(print-interval (mul-interval i1 i2))
(newline)
(print-interval (div-interval i1 i2))
(newline)
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (make-interval a b) (cons a b))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(define (width i) (- (upper-bound i) (lower-bound i)))
(define (center i) (+ (lower-bound i) (/ (width i) 2)))
(define (percent i) (/ (/ (width i) 2) (center i)))
(define i1 (make-interval 9.5 10.5))
(define i2 (make-interval 1.8 2.2))
(define i3 (mul-interval i1 i2))
(display (percent i1))
(newline)
(display (percent i2))
(newline)
(display (+ (percent i1) (percent i2)))
(newline)
(display (percent i3))
(newline)
(display (- (percent i3) (+ (percent i1) (percent i2))))
(newline)
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (div-interval x y)
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
(define (make-interval a b) (cons a b))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(define (print-interval i)
(display "(")
(display (lower-bound i))
(display ",")
(display (upper-bound i))
(display ")"))
(define (par1 r1 r2)
(div-interval (mul-interval r1 r2)
(add-interval r1 r2)))
(define (par2 r1 r2)
(let ((one (make-interval 1 1)))
(div-interval one
(add-interval (div-interval one r1)
(div-interval one r2)))))
(define i1 (make-interval 9.5 10.5))
(define i2 (make-interval 1.8 2.2))
(print-interval (par1 i1 i2))
(newline)
(print-interval (par2 i1 i2))
(newline)
(define (make-point x y) (cons x y))
(define (x-point p) (car p))
(define (y-point p) (cdr p))
(define (make-segment start end) (cons start end))
(define (start-segment l) (car l))
(define (end-segment l) (cdr l))
(define (midpoint-segment l)
(let ((start (start-segment l)) (end (end-segment l)))
(let ((xstart (x-point start))
(ystart (y-point start))
(xend (x-point end))
(yend (y-point end)))
(make-point (+ xstart (/ (- xend xstart) 2))
(+ ystart (/ (- yend ystart) 2))))))
(define (print-point p)
(display "(")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")"))
(define (print-line l)
(print-point (start-segment l))
(display " -> ")
(print-point (end-segment l)))
(define l1 (make-segment (make-point 0 0) (make-point 2 4)))
(print-line l1)
(newline)
(print-point (midpoint-segment l1))
(newline)
(define l2 (make-segment (make-point 3 1) (make-point 4 1)))
(print-line l2)
(newline)
(print-point (midpoint-segment l2))
(newline)
(define (make-point x y) (cons x y))
(define (x-point p) (car p))
(define (y-point p) (cdr p))
(define (make-segment start end) (cons start end))
(define (start-segment l) (car l))
(define (end-segment l) (cdr l))
(define (segment-length l)
(let ((start (start-segment l)) (end (end-segment l)))
(let ((dx (- (x-point end) (x-point start)))
(dy (- (y-point end) (y-point start))))
(sqrt (+ (* dx dx) (* dy dy))))))
(define (make-rect bt-left up-right) (cons bt-left up-right))
(define (rect-bt-left r) (car r))
(define (rect-bt-right r) (make-point (x-point (cdr r))
(y-point (car r))))
(define (rect-up-right r) (cdr r))
(define (rect-up-left r) (make-point (x-point (car r))
(y-point (cdr r))))
(define (rect-perimeter r)
(let ((bt-left (rect-bt-left r))
(bt-right (rect-bt-right r))
(up-right (rect-up-right r))
(up-left (rect-up-left r)))
(+ (segment-length (make-segment bt-left bt-right))
(segment-length (make-segment bt-right up-right))
(segment-length (make-segment up-right up-left))
(segment-length (make-segment up-left bt-left)))))
(define (rect-area r)
(let ((bt-left (rect-bt-left r))
(bt-right (rect-bt-right r))
(up-left (rect-up-left r)))
(* (- (x-point bt-right) (x-point bt-left))
(- (y-point up-left) (y-point bt-left)))))
(define r1 (make-rect (make-point 1 2) (make-point 5 4)))
(display (rect-perimeter r1))
(newline)
(display (rect-area r1))
(newline)
(define (make-rect c w h) (cons c (cons w h)))
(define (rect-bt-left r)
(let ((c (car r)) (w (car (cdr r))) (h (cdr (cdr r))))
(make-point (- (x-point c) (/ w 2))
(- (y-point c) (/ h 2)))))
(define (rect-bt-right r)
(let ((c (car r)) (w (car (cdr r))) (h (cdr (cdr r))))
(make-point (+ (x-point c) (/ w 2))
(- (y-point c) (/ h 2)))))
(define (rect-up-right r)
(let ((c (car r)) (w (car (cdr r))) (h (cdr (cdr r))))
(make-point (+ (x-point c) (/ w 2))
(+ (y-point c) (/ h 2)))))
(define (rect-up-left r)
(let ((c (car r)) (w (car (cdr r))) (h (cdr (cdr r))))
(make-point (- (x-point c) (/ w 2))
(+ (y-point c) (/ h 2)))))
(define r1 (make-rect (make-point 3 3) 4 2))
(display (rect-perimeter r1))
(newline)
(display (rect-area r1))
(newline)
(define (cons x y)
(lambda (m) (m x y)))
(define (car z)
(z (lambda (p q) p)))
(define (cdr z)
(z (lambda (p q) q)))
(define c (cons 1 2))
(display (car c))
(newline)
(display (cdr c))
(newline)
(define (cons x y) (* (expt 2 x) (expt 3 y)))
(define (car z) (count-perfect-divisions z 2))
(define (cdr z) (count-perfect-divisions z 3))
(define (count-perfect-divisions n d)
(define (iter x i)
(if (not (= (remainder x d) 0))
i
(iter (/ x d) (+ i 1))))
(iter n 0))
(define c (cons 14 15))
(display (car c))
(newline)
(display (cdr c))
(newline)
(define zero (lambda (f) (lambda (x) x)))
(define (add-1 n)
(lambda (f)
(lambda (x)
(f ((n f) x)))))
(define one (lambda (f) (lambda (x) (f x))))
(define two (lambda (f) (lambda (x) (f (f x)))))
(define (add a b)
(lambda (f)
(lambda (x)
((a f) ((b f) x)))))
(display ((one square) 2))
(newline)
(display ((two square) 2))
(newline)
(display (((add two one) square) 2))
(newline)
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (div-interval x y)
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
(define (make-interval a b) (cons a b))