2020-10-20 03:36:17 +02:00
|
|
|
(load "util.scm")
|
|
|
|
|
|
|
|
(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 (div-rat x y)
|
|
|
|
(make-rat (* (numer x) (denom y))
|
|
|
|
(* (denom x) (numer y))))
|
|
|
|
(define (equal-rat? x y)
|
|
|
|
(= (* (numer x) (denom y))
|
|
|
|
(* (numer y) (denom x))))
|
|
|
|
|
|
|
|
(define (make-rat n d)
|
|
|
|
(let ((g (gcd n d)))
|
|
|
|
(cons (/ n g) (/ d g))))
|
|
|
|
(define (numer x) (car x))
|
|
|
|
(define (denom x) (cdr x))
|
|
|
|
|
|
|
|
(define (print-rat x)
|
|
|
|
(newline)
|
|
|
|
(display (numer x))
|
|
|
|
(display "/")
|
|
|
|
(display (denom x)))
|
|
|
|
|
|
|
|
; Examples
|
|
|
|
; (define one-half (make-rat 1 2))
|
|
|
|
; (print-rat one-half)
|
|
|
|
; (define one-third (make-rat 1 3))
|
|
|
|
; (print-rat (add-rat one-half one-third))
|
|
|
|
; (print-rat (mul-rat one-half one-third))
|
|
|
|
; (print-rat (add-rat one-third one-third))
|
|
|
|
|
|
|
|
(display "ex-2.1")
|
|
|
|
|
|
|
|
(define (make-rat n d)
|
|
|
|
(let ((g (gcd n d)))
|
|
|
|
(if (< (* n d) 0)
|
|
|
|
(cons (- (abs (/ n g))) (abs (/ d g)))
|
|
|
|
(cons (abs (/ n g)) (abs (/ d g))))))
|
|
|
|
|
|
|
|
(print-rat (make-rat 3 9))
|
|
|
|
(print-rat (make-rat -3 9))
|
|
|
|
(print-rat (make-rat 3 -9))
|
|
|
|
(print-rat (make-rat -3 -9))
|
|
|
|
|
|
|
|
(display "\n\nex-2.2")
|
|
|
|
|
2020-10-24 17:24:13 +02:00
|
|
|
(define (make-point x y) (cons x y))
|
|
|
|
(define (x-point p) (car p))
|
|
|
|
(define (y-point p) (cdr p))
|
|
|
|
|
|
|
|
(define (make-segment a b) (cons a b))
|
|
|
|
(define (start-segment s) (car s))
|
|
|
|
(define (end-segment s) (cdr s))
|
|
|
|
|
|
|
|
(define (midpoint-segment s)
|
|
|
|
(make-point (average (x-point (start-segment s)) (x-point (end-segment s)))
|
|
|
|
(average (y-point (start-segment s)) (y-point (end-segment s)))))
|
|
|
|
|
|
|
|
(define (print-point p)
|
|
|
|
(newline)
|
|
|
|
(display "(")
|
|
|
|
(display (x-point p))
|
|
|
|
(display ", ")
|
|
|
|
(display (y-point p))
|
|
|
|
(display ")"))
|
|
|
|
|
|
|
|
(define s (make-segment (make-point 1 2) (make-point 7 4)))
|
|
|
|
(print-point (midpoint-segment s))
|
|
|
|
|
|
|
|
(display "\n\nex-2.3\n")
|
|
|
|
|
|
|
|
; The first representation takes the two opposite corners of the rectangle.
|
|
|
|
(define (make-rectangle p1 p2) (cons p1 p2))
|
|
|
|
(define (corner-1-rectangle r) (car r))
|
|
|
|
(define (corner-2-rectangle r) (cdr r))
|
|
|
|
|
|
|
|
(define (area-rectangle r)
|
|
|
|
(abs (* (- (x-point (corner-1-rectangle r)) (x-point (corner-2-rectangle r)))
|
|
|
|
(- (y-point (corner-1-rectangle r)) (y-point (corner-2-rectangle r))))))
|
|
|
|
|
|
|
|
(define (perimeter-rectangle r)
|
|
|
|
(* 2 (+ (abs (- (x-point (corner-1-rectangle r)) (x-point (corner-2-rectangle r))))
|
|
|
|
(abs (- (y-point (corner-1-rectangle r)) (y-point (corner-2-rectangle r)))))))
|
|
|
|
|
|
|
|
(define r (make-rectangle (make-point -2 -2) (make-point -8 -10)))
|
|
|
|
(display (area-rectangle r)) (newline)
|
|
|
|
(display (perimeter-rectangle r)) (newline)
|
|
|
|
|
|
|
|
; The second representation takes one corner and the size of the rectangle.
|
|
|
|
; The consequence is that we have to calculate the second point for the
|
|
|
|
; corner-2 getter.
|
|
|
|
(define (make-rectangle p1 size) (cons p1 size))
|
|
|
|
(define (corner-1-rectangle r) (car r))
|
|
|
|
(define (corner-2-rectangle r)
|
|
|
|
(make-point (+ (x-point (car r)) (x-point (cdr r)))
|
|
|
|
(+ (y-point (car r)) (y-point (cdr r)))))
|
|
|
|
|
|
|
|
; Our higher level functions still deliver the same result even though the
|
|
|
|
; underlying presentation of the rectangle is different.
|
|
|
|
(define r (make-rectangle (make-point -2 -2) (make-point -6 -8)))
|
|
|
|
(display (area-rectangle r)) (newline)
|
|
|
|
(display (perimeter-rectangle r)) (newline)
|
|
|
|
|
|
|
|
(display "\nex-2.4\n")
|