sicp

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

commit c13f497cdf68d53ce087cc1e57db6b3047d46616
parent e78d5ca3a563077784a0f315db0754a02a38ec06
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 14 Oct 2017 03:10:24 +0000

(net ricketyspace sicp one thirtyone): Add iterative version.

* net/ricketyspace/sicp/one/thirtyone.scm
(product-iter, factorial-iter, pi-iter): New functions.

Diffstat:
net/ricketyspace/sicp/one/thirtyone.scm | 52++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 42 insertions(+), 10 deletions(-)

diff --git a/net/ricketyspace/sicp/one/thirtyone.scm b/net/ricketyspace/sicp/one/thirtyone.scm @@ -3,7 +3,7 @@ ;;;; <https://creativecommons.org/licenses/by-sa/4.0/>. (define-module (net ricketyspace sicp one thirtyone) - #:export (product factorial pi)) + #:export (product product-iter factorial factorial-iter pi pi-iter)) (define (product term a next b) (if (> a b) @@ -11,11 +11,23 @@ (* (term a) (product term (next a) next b)))) +(define (product-iter term a next b) + (define (iter a result) + (if (> a b) + result + (iter (next a) (* (term a) result)))) + (iter a 1)) + (define (factorial n) (let ((term (lambda (x) x)) (next (lambda (x) (1+ x)))) (product term 1 next n))) +(define (factorial-iter n) + (let ((term (lambda (x) x)) + (next (lambda (x) (1+ x)))) + (product-iter term 1 next n))) + (define (pi precision) (let* ((n (lambda (x) (* (+ (quotient x 2) 1) 2))) ; numerator of term. (d (lambda (x) (+ (* (quotient (1+ x) 2) 2) 1))) ; denominator of term. @@ -23,21 +35,41 @@ (next (lambda (x) (1+ x)))) (* (product term 1 next precision) 4))) +(define (pi-iter precision) + (let* ((n (lambda (x) (* (+ (quotient x 2) 1) 2))) ; numerator of term. + (d (lambda (x) (+ (* (quotient (1+ x) 2) 2) 1))) ; denominator of term. + (term (lambda (x) (/ (* (n x) 1.0) (* (d x) 1.0)))) + (next (lambda (x) (1+ x)))) + (* (product-iter term 1 next precision) 4))) + + ;;; Guile REPL ;;; scheme@(guile-user)> ,use (net ricketyspace sicp one thirtyone) ;;; scheme@(guile-user)> (factorial 5) -;;; $9 = 120 +;;; $19 = 120 +;;; scheme@(guile-user)> (factorial-iter 5) +;;; $20 = 120 ;;; scheme@(guile-user)> (factorial 10) -;;; $7 = 3628800 +;;; $21 = 3628800 +;;; scheme@(guile-user)> (factorial-iter 10) +;;; $22 = 3628800 ;;; scheme@(guile-user)> (factorial 20) -;;; $8 = 2432902008176640000 +;;; $23 = 2432902008176640000 +;;; scheme@(guile-user)> (factorial-iter 20) +;;; $24 = 2432902008176640000 ;;; scheme@(guile-user)> (pi 1000) -;;; $10 = 3.143160705532257 +;;; $25 = 3.143160705532257 +;;; scheme@(guile-user)> (pi-iter 1000) +;;; $26 = 3.1431607055322552 ;;; scheme@(guile-user)> (pi 10000) -;;; $11 = 3.1417497057379635 +;;; $27 = 3.1417497057379635 +;;; scheme@(guile-user)> (pi-iter 10000) +;;; $28 = 3.1417497057380084 ;;; scheme@(guile-user)> (pi 100000) -;;; $12 = 3.1416083612780903 +;;; $29 = 3.1416083612780903 +;;; scheme@(guile-user)> (pi-iter 100000) +;;; $30 = 3.141608361277941 ;;; scheme@(guile-user)> (pi 1000000) -;;; $13 = 3.1415942243828017 - - +;;; $31 = 3.1415942243828017 +;;; scheme@(guile-user)> (pi-iter 1000000) +;;; $32 = 3.141594224382854