1 day ago
my attempt to do the exercises in sicp.
Sunday, July 6, 2008
sicp exercise 2.1
; 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.
(begin
(define (display-rat rat)
(display (car rat))
(display '/)
(display (cdr rat))
(newline))
(define (make-rat num den)
(define (getsign num)
(if (> num 0) 1 (- 1)))
(define (makeplus a)
(if (> a 0) a (- a)))
(cons
(*
(*
(getsign num)
(getsign den))
(makeplus num))
(makeplus den)))
(display-rat (make-rat 2 3))
(display-rat (make-rat 2 -3))
(display-rat (make-rat -2 3))
(display-rat (make-rat -2 -3))
)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment