在SICP练习1.37中
第1.3.3节滚动到部分的末尾(就在1.3.4之前),以在本节中找到第三次练习。
根据问题,我将cont-frac定义为
(define (cont-frac n d k)
(if (= k 0)
0
(/ n (+ d (cont-frac n d (- k 1))))
)
)根据解决方案链接,上述代码似乎是一致的。问题出现在解的第二部分,当n和d被替换为(lamda (i) 1.0)时,在解的(a)部分,这是一个过程。
我不明白如果在cont-frac的过程中替代它,它将如何工作。当我尝试时,出现了错误类型的论点。
编辑1
我把我的全部解决方案都加进去了。它解决了问题,但没有抓住这一节的本质。这是练习1.37、1.38和1.39的解决方案。该程序不使用过程作为通用方法,以下链接的解决方案用于1.37的解决办法、1.38的解决办法和1.39的解决办法
在下面的程序中
在phi和e-2-val过程中,k不是连续分式中的步骤之一。
在程序tan中,k是弧度角(对于精确值,步骤号为1000 )
#!/usr/local/bin/guile \
-e main -s
!#
(define (finite-cont-frac n d k)
(if (= k 0)
0
(/ n (+ d (finite-cont-frac n d (- k 1))))))
(define (e-2 n d k1 c k)
(define (d-val)
(if (= (modulo k1 3) 1)
(+ c 2)
1))
(define (c-val)
(if (= (d-val) 1) c (d-val)))
(if (= k 0)
0
(/ n (+ (d-val) (e-2 n (d-val) (+ k1 1) (c-val) (- k 1))))))
(define (tan-cf n k d k1)
(define (d-val)
(if (= k1 0) 1 (+ d 2)))
(if (= k 0)
0
(/ n (+ (d-val) (tan-cf n (- k 1) (d-val) (+ k1 1))))))
(define (tan-man x kk)
(let ((a (- (* x x))))
(tan-cf a kk 1 0)))
(define rrr 80.0)
(define (main args)
(let* ((k (string->number (list-ref args 1)))
(phi (/ 1.0 (finite-cont-frac 1.0 1.0 k)))
(e-2-val (e-2 1.0 1 0.0 0 k))
(tt (/ (tan-man k 1000) (- 0.0 k))))
(display tt)
(newline)))发布于 2016-07-13 20:32:49
链接的答案看起来是错误的,你应该做通过程序,而不是数字作为实际参数。使用名为accumulate的辅助过程
(define (accumulate combiner null-value term1 term2 a next b)
(if (> a b)
null-value
(combiner (term1 a)
(term2 a)
(accumulate combiner
null-value
term1
term2
(next a)
next
b))))
(define (cont-frac n d k)
(accumulate (λ (x y rec) (/ x (+ y rec)))
0 n d 1 add1 k))现在,我们可以按预期调用该过程:
(cont-frac (lambda (i) 1.0)
(lambda (i) 1.0)
10)
=> 0.6179775280898876https://stackoverflow.com/questions/38361004
复制相似问题