sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit 91f66f94ed94f63db3a8c9f45a564d10d8388aea
parent 65bfbb936e608d0c8654f3117b9c5cdb4763505f
Author: rsiddharth <s@ricketyspace.net>
Date:   Sun, 28 Jan 2018 00:28:23 +0000

Update net/ricketyspace/sicp/one/thirtyseven.scm.

Diffstat:
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 @@ -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