(define (make-rat n d) (cons n d));;返回一个有理书,其分子是整数n,分母是整数d.
(define (number x) (car x));;返回有理数x的分子.
(define (denom x) (cdr x));;返回有理数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))))
;;打印分子,在/之后打印分母.
(define (print-rat x)
(newline)
(display "/")
(display (denom x)))
;;过程
(define onr-half (make-rat 1 2))
(print-rat one-half)
(define one-third (make-rat 1 3))
(print-rat (add-rat one-half one-third))
(print-rat (mul-rat one-half one-third))
(print-rat (add-rat one-third one-third))
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
(print-rat (add-rat one-third one-third))
2010年5月29日星期六
订阅:
博文评论 (Atom)
没有评论:
发表评论