Skip to content

Instantly share code, notes, and snippets.

@faust45
Created February 1, 2019 11:20
Show Gist options
  • Save faust45/2381c80cfc7e6033ebe5518c7ebc5702 to your computer and use it in GitHub Desktop.
Save faust45/2381c80cfc7e6033ebe5518c7ebc5702 to your computer and use it in GitHub Desktop.
#lang racket/load
(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) (cons n d))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (print-rat x)
(newline)
(display (numer x))
(display "/")
(display (denom x)))
(define a (make-rat 1 2))
(define b (make-rat 1 3))
(print-rat (add-rat a b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment