Download presentation
Presentation is loading. Please wait.
Published byLeanna Marston Modified over 9 years ago
1
(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))
2
(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))))
3
(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)))))
4
(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))))
5
(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))))
6
(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 "...")))
7
(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))))
8
(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)))))
9
(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))))
10
(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
© 2025 SlidePlayer.com. Inc.
All rights reserved.