# flaming0/SICP

### Subversion checkout URL

You can clone with
or
.
Fetching contributors…

Cannot retrieve contributors at this time

53 lines (40 sloc) 1.13 KB
 ;; Example 2.1 (define (make-rat n d) (let ((g (gcd n d))) (if (< d 0) (cons (/ (- n) g) (/ (- d) g)) (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))) ;; methods (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)))) ;; test (define min-one-half-1 (make-rat -1 -2)) (print-rat min-one-half-1) (define min-one-half-2 (make-rat -1 2)) (print-rat min-one-half-2) (define min-one-half-3 (make-rat 1 -2)) (print-rat min-one-half-3) (define min-one-half-4 (make-rat 1 2)) (print-rat min-one-half-4)
Something went wrong with that request. Please try again.