summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace
diff options
context:
space:
mode:
Diffstat (limited to 'net/ricketyspace')
-rw-r--r--net/ricketyspace/sicp/one/thirtyseven.scm25
1 files changed, 18 insertions, 7 deletions
diff --git a/net/ricketyspace/sicp/one/thirtyseven.scm b/net/ricketyspace/sicp/one/thirtyseven.scm
index 09081d4..ad257c7 100644
--- a/net/ricketyspace/sicp/one/thirtyseven.scm
+++ b/net/ricketyspace/sicp/one/thirtyseven.scm
@@ -3,20 +3,31 @@
;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
(define-module (net ricketyspace sicp one thirtyseven)
- #:export (cont-frac-iter))
+ #:export (cont-frac-iter
+ cont-frac-rec))
-(define (cont-frac-iter n d k)
- (define (do-iter n d i)
+(define (cont-frac-rec n d k)
+ (define (do-rec n d i)
(cond ((< i k) (/ (n i)
- (+ (d i) (do-iter n d (1+ i)))))
+ (+ (d i) (do-rec n d (1+ i)))))
(else (/ (n i) (d i)))))
- (do-iter n d 1))
+ (do-rec n d 1))
+
+(define (cont-frac-iter n d k)
+ (define (do-iter n d i acc)
+ (cond ((= i 0) acc)
+ (else (do-iter n d (1- i) (/ (n i) (+ (d i) acc))))))
+ (do-iter n d k 0))
;;; Guile REPL
;;;
;;; scheme@(guile-user)> ,use (net ricketyspace sicp one thirtyseven)
+;;; scheme@(guile-user)> (cont-frac-rec (lambda (i) 1.0) (lambda (i) 1.0) 10)
+;;; $7 = 0.6179775280898876
;;; scheme@(guile-user)> (cont-frac-iter (lambda (i) 1.0) (lambda (i) 1.0) 10)
-;;; $16 = 0.6179775280898876
+;;; $8 = 0.6179775280898876
+;;; scheme@(guile-user)> (cont-frac-rec (lambda (i) 1.0) (lambda (i) 1.0) 11)
+;;; $9 = 0.6180555555555556
;;; scheme@(guile-user)> (cont-frac-iter (lambda (i) 1.0) (lambda (i) 1.0) 11)
-;;; $17 = 0.6180555555555556
+;;; $10 = 0.6180555555555556