From 91f66f94ed94f63db3a8c9f45a564d10d8388aea Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sun, 28 Jan 2018 00:28:23 +0000 Subject: Update net/ricketyspace/sicp/one/thirtyseven.scm. --- net/ricketyspace/sicp/one/thirtyseven.scm | 25 ++++++++++++++++++------- 1 file 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 @@ ;;;; . (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 -- cgit v1.2.3