2.1.1

Announce/Chapter.2 2011.01.01 11:05
; 2.1.1

; operation
(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))
            (* (numer y) (denom x))))
(define (equal-rat? x y)
  (= (* (numer x) (denom y))
     (* (numer y) (denom x))))

; constructor, selector
(define (make-rat n d) (cons n d))
(define (numer x) (car x))
(define (denom x) (cdr x))

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

(define (make-rat2 n d)
  (let ((g (gcd n d)))
    (cons (/ n g) (/ d g))))
신고

...
신고


(define (cont-frac n d k)
  (define (cont-frac-helper i)
    (if (> i k)
        0
        (/ (n i) (+ (d i) (cont-frac-helper (+ i 1))))))
  (cont-frac-helper 1))

(define (ln-cf k)
  (define (n k) 1.0)
  (define (d k) (if (= (remainder (+ k 1) 3) 0) (* (/ (+ k 1) 3) 2) 1))
  (cont-frac n d k))

(ln-cf 10)

(define (tan-cf x k)
  (define (n k) (if ( = k 1) x (* x x)))
  (define (d k) (- (* 2 k) 1))
  (cont-frac n d k))

(tan-cf 0 10)
환영합니다. DrRacket, 버전 5.0.2 [3m].
언어: R5RS [사용자정의]; memory limit: 256 MB.
0.7182817182817183
0
>
신고

티스토리 툴바