ITeye博客

SICP 2.1 答案

t0uch · 2009-12-11 16:44 · SICP · 编程语言 · Markdown

(define (gcd x y) ;; we've implemented gcd in section 1.2.5.
  (if (= y 0)
      x
      (gcd y (remainder x y))))

(define (signum x)
  (if (= 0 x)
      0
      (/ x (abs x))))

(define (make-rat n d)
  (let ((g (gcd (abs n) (abs d)))
        (s (signum d)))
    (cons (/ n (* g s)) 
          (/ d (* g s)))))

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。