Created
October 13, 2020 12:27
-
-
Save jfacoustic/c04300971bcf4ee830e6ace5fe1837fc to your computer and use it in GitHub Desktop.
1.31 SICP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (product term a next b) | |
(define (iter a result) | |
(if (= a b) | |
result | |
(iter (next a) (* result (term a))))) | |
(iter a 1)) | |
(define (inc x) (+ x 1)) | |
(define (identity x) x) | |
(define (factorial n) | |
(product identity 2 inc n)) | |
(define (square x) (* x x)) | |
(define (pi-approx n) | |
(exact->inexact | |
(* 4 | |
(/ (* 2 (product | |
(lambda (x) | |
(if (= x n) (* 2 x) (square (* 2 x)))) 2 inc n)) | |
(product (lambda (x) (square (+ (* x 2) 1))) 1 inc (- n 1)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment