-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrational.rkt
45 lines (36 loc) · 1.02 KB
/
rational.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#lang racket
(require "pair-rational.rkt")
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (denom x) (numer y)))
(* (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (- (* (numer x) (denom y))
(* (denom x) (numer y)))
(* (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))
(* (denom x) (numer y))))
(define (print-rat x)
(display (numer x))
(display "/")
(display (denom x))
(newline))
(define one-half (make-rat 1 2))
(define one-third (make-rat 1 3))
(print-rat one-half)
(print-rat one-third)
(print-rat (add-rat one-half one-third))
(print-rat (make-rat 0 -1))
(print-rat (make-rat 1 -1))
(print-rat (make-rat -2 -2))
(print-rat (make-rat 2 3))
(print-rat (make-rat 2 -4))
(print-rat (make-rat 0 4))
(print-rat (make-rat -2 4))