SICP-1.3.4节练习
SICP-2.1.2节练习

SICP-2.1.1节练习

lispor posted @ Feb 24, 2011 04:56:16 AM in Scheme with tags SICP , 1415 阅读

练习 2.1

 
练习 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.
我的解答:
(define (make-rat n d)
  (let ((g (gcd n d)))
    (cond ((positive? d)
           (cons (/ n g) (/ d g)))
          ((negative? d)
           (cons (- (/ n g)) (- (/ d g))))
          (else
           (error "The denom is Zero!")))))

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter