码迷,mamicode.com
首页 > 其他好文 > 详细

2.1

时间:2018-02-01 00:08:00      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:new   blog   let   line   span   style   pos   int   class   

(define (make-rat n d)
  (let ((g (gcd n d)))
    (cons (/ n g) (/ d g))))

(define (numer x) (car x))
(define (denom x) (cdr x))

(define (print-rat x)
  (newline)
  (cond ((= (denom x) 1) (display (numer x)))
      ((display (numer x))
      (display "/")
      (display (denom x)))
      ))

(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)
  (newline)
  (= (* (numer x) (denom y))
     (* (denom x) (numer y))))

(define one-half (make-rat 1 2))
(print-rat one-half)

(define one-third (make-rat 1 3))
(print-rat one-third)

(print-rat (add-rat one-half one-third))
(print-rat (mul-rat one-half one-third))
(print-rat (sub-rat one-half one-third))
(print-rat (div-rat one-half one-third))
(equal-rat? one-half one-third)
(print-rat (add-rat one-third one-third))
(print-rat (make-rat 2 1))
(print-rat (make-rat 4 2))
(print-rat (make-rat 2 2))


;2.1
(print-rat (sub-rat one-third one-half))
(print-rat (sub-rat one-third one-third))

 

2.1

标签:new   blog   let   line   span   style   pos   int   class   

原文地址:https://www.cnblogs.com/R4mble/p/8395011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!