Created
February 1, 2019 11:20
-
-
Save faust45/2381c80cfc7e6033ebe5518c7ebc5702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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