Skip to content

Instantly share code, notes, and snippets.

@yanfeng42
Created June 28, 2021 06:36
Show Gist options
  • Save yanfeng42/d89b122c7cc390688390c0189d04e32f to your computer and use it in GitHub Desktop.
Save yanfeng42/d89b122c7cc390688390c0189d04e32f to your computer and use it in GitHub Desktop.
sicp 1.39 用单独符号, 还是用 系数 表达 "+" 和 "-"
; 我的答案:
; 递归写法.
(define (cont-frac-full n d k calculator)
(define (iterator i)
(if (< i k)
(/ (n i) (calculator (d i) (iterator (+ i 1))))
(/ (n i) (d i))
)
)
(iterator 1)
)
; 迭代写法.
(define (cont-frac-full n d k calculator)
(define (iterator i product)
(if (= i 0)
product
(iterator (- i 1) (/ (n i) (calculator (d i) product)))
)
)
(iterator k 0)
)
(define (square x) (* x x))
(define (tan-cf x k)
(cont-frac-full
(lambda (i) (if (= i 1) x (square x)))
(lambda (i) (- (* 2 i) 1))
k
-
)
)
#|
> (tan-cf (/ 3.14 4) 100)
0.9992039901050428
|#
#| 社区答案:
> (tan-cf (/ 3.14 4) 100)
0.9992039901050428
|#
(define (tan-cf x k)
(cont-frac (lambda (i)
(if (= i 1) x (- (* x x))))
(lambda (i)
(- (* i 2) 1))
k))
@yanfeng42
Copy link
Author

核心区别是:

  • 我的解法, 是把 D 之后的 "+" 和 "-", 作为一个单独的参数, 交由外部控制.
  • 社区解法, 是把 D 之后的 "+" 和 "-", 作为后一项的 分子的 相对符号. 即: (- (* x x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment