# (define (make-rat n d) (if (and (number? n) (number? d) (not (= d 0))) (cons n d) (error "cannot make a rat with denominator 0"))) (define (numer r) (head.

## Presentation on theme: "(define (make-rat n d) (if (and (number? n) (number? d) (not (= d 0))) (cons n d) (error "cannot make a rat with denominator 0"))) (define (numer r) (head."— Presentation transcript:

(define (make-rat n d) (if (and (number? n) (number? d) (not (= d 0))) (cons n d) (error "cannot make a rat with denominator 0"))) (define (numer r) (head r)) (define (denom r) (tail r))

(define (rat-add r1 r2) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (make-rat (+ (* n1 d2) (* n2 d1)) (* d1 d2)))) (define (rat-mul r1 r2) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (make-rat (* n1 n2) (* d1 d2))))

(define (rat-eq r1 r2) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (= (* n1 d2) (* n2 d1)))) (define (rat-leq r1 r2) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (if (>= (* d1 d2) 0) ;;if d1 and d2 have the same sign ( = (* n1 d2) (* n2 d1)))))

(define (make-rat n d) (if (and (number? n) (number? d) (not (= d 0))) (let ((g (gcd n d))) (cons (/ n g) (/ d g))) (error "..."))) (define (rat-eq r1 r2) (and (= (numer r1) (numer r2)) (= (denom r1) (denom r2))))

(define (make-rat n d) (if (and (number? n) (number? d) (not (= d 0))) (let* ((n2 (if (> d 0) n (- n))) (d2 (if (> d 0) d (- d))) (g (gcd n2 d2))) (cons (/ n2 g) (/ d2 g))) (error "..."))) (define (rat-leq r1 r2) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (<= (* n1 d2) (* n2 d1))))

(defstruct (n ) (d )) (define (numer (r )) (get-rat-n r)) (define (denom (r )) (get-rat-d r)) (define (new-rat (n ) (d )) (if (not (zero? d)) (let ((g (gcd n d))) (make-rat (/ n g) (/ d g))) (error "...")))

(define (rat-add (r1 ) (r2 )) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (new-rat (+ (* n1 d2) (* n2 d1)) (* d1 d2)))) (define (rat-mul (r1 ) (r2 )) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (new-rat (* n1 n2) (* d1 d2))))

(define (rat-eq (r1 ) (r2 )) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (= (* n1 d2) (* n2 d1)))) (define (rat-leq (r1 ) (r2 )) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (if (>= (* d1 d2) 0) ;;if d1 and d2 have the same sign ( = (* n1 d2) (* n2 d1)))))

(define (new-rat (n ) (d )) (if (not (= d 0)) (let ((g (gcd n d))) (make-rat (/ n g) (/ d g))) (error "..."))) (define (rat-eq (r1 ) (r2 )) (and (= (numer r1) (numer r2)) (= (denom r1) (denom r2))))

(define (new-rat (n ) (d )) (if (not (= d 0)) (let* ((n2 (if (> d 0) n (- n))) (d2 (if (> d 0) d (- d))) (g (gcd n2 d2))) (make-rat (/ n2 g) (/ d2 g))) (error "..."))) (define rat-leq (r1 ) (r2 )) (let ((n1 (numer r1)) (d1 (denom r1)) (n2 (numer r2)) (d2 (denom r2))) (<= (* n1 d2) (* n2 d1))))

Similar presentations