Skip to content

Instantly share code, notes, and snippets.

@jfacoustic
Created October 13, 2020 12:27
Show Gist options
  • Save jfacoustic/c04300971bcf4ee830e6ace5fe1837fc to your computer and use it in GitHub Desktop.
Save jfacoustic/c04300971bcf4ee830e6ace5fe1837fc to your computer and use it in GitHub Desktop.
1.31 SICP
(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