summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2018-01-28 00:28:23 +0000
committerrsiddharth <s@ricketyspace.net>2018-01-28 00:28:23 +0000
commit91f66f94ed94f63db3a8c9f45a564d10d8388aea (patch)
tree03475ca05b8643faeb8d30f8db8e658a2d6a595f /net
parent65bfbb936e608d0c8654f3117b9c5cdb4763505f (diff)
Update net/ricketyspace/sicp/one/thirtyseven.scm.
Diffstat (limited to 'net')
-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