Skip to content

Instantly share code, notes, and snippets.

@jfacoustic
Last active October 13, 2020 12:22
Show Gist options
  • Save jfacoustic/fe447d2af34fabd497716cb93e2cc902 to your computer and use it in GitHub Desktop.
Save jfacoustic/fe447d2af34fabd497716cb93e2cc902 to your computer and use it in GitHub Desktop.
1.31 SICP (recursive)
(define (product term a next b)
(if (> a b)
1
(* (term a)
(product term (next a) next b))))
(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