sicp

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

commit 71b1de4198dad80ffc8755c34aa48a39ef512226
parent c13f497cdf68d53ce087cc1e57db6b3047d46616
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 14 Oct 2017 03:33:06 +0000

(net ricketyspace sicp one thirtyone): Simplify pi functions.

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

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

diff --git a/net/ricketyspace/sicp/one/thirtyone.scm b/net/ricketyspace/sicp/one/thirtyone.scm @@ -29,18 +29,18 @@ (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. - (term (lambda (x) (/ (* (n x) 1.0) (* (d x) 1.0)))) + (let* ((n (lambda (x) (if (even? x) (+ x 2) (+ x 1)))) ; numerator of term. + (d (lambda (x) (if (even? x) (+ x 1) (+ x 2)))) ; denominator of term. + (term (lambda (x) (/ (n x) (d x)))) (next (lambda (x) (1+ x)))) - (* (product term 1 next precision) 4))) + (* (product term 1.0 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)))) + (let* ((n (lambda (x) (if (even? x) (+ x 2) (+ x 1)))) ; numerator of term. + (d (lambda (x) (if (even? x) (+ x 1) (+ x 2)))) ; denominator of term. + (term (lambda (x) (/ (n x) (d x)))) (next (lambda (x) (1+ x)))) - (* (product-iter term 1 next precision) 4))) + (* (product-iter term 1.0 next precision) 4))) ;;; Guile REPL