Exercise 2.1: Define a better version of make-rat
that handles both positive and negative
arguments. Make-rat should normalize the sign so that if the rational number is positive, both
the numerator and denominator are positive, and if the rational number is negative, only the
numerator is negative.
(define (numer x) (car x))
(define (denom x) (cdr x))
(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)))
(if (< d 0)
(make-rat (- n) (- d))
(cons (/ n g)
(/ d g)))))
(define (print-rat x)
(newline)
(display (numer x))
(display "/")
(display (denom x)))