Skip to content

Instantly share code, notes, and snippets.

@kouddy
Created March 22, 2015 01:42
Show Gist options
  • Save kouddy/ff78de478c01f65aabb4 to your computer and use it in GitHub Desktop.
Save kouddy/ff78de478c01f65aabb4 to your computer and use it in GitHub Desktop.
;;; Assume n and d are never 0, else gcd will return 0, which causes division by zero
(define (make-rat n d)
(define g (gcd n d))
(if (or (and (< n 0) (< d 0)) (and (> n 0) (> d 0)))
(cons (/ (abs n) g) (/ (abs d) g))
(cons (/ n g) (/ d g))))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (print-rat x)
(display (numer x))
(display "/")
(display (denom x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment