;构建有理数
(define (make-rat n d)
(let ((g (gcd (abs n) (abs d))))
(if (< (* n d) 0)
(cons (- 0 (abs (/ n g))) (abs (/ d g)))
(cons (abs (/ n g)) (abs (/ d g))))))
;取得分子
(define (numer x) (car x))
;取得分母
(define (denom x) (cdr 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)
(= (* (numer x) (denom y))
(* (numer y) (denom x))))
2.1
最新推荐文章于 2024-10-17 11:49:30 发布