Last active
October 13, 2020 12:22
-
-
Save jfacoustic/fe447d2af34fabd497716cb93e2cc902 to your computer and use it in GitHub Desktop.
1.31 SICP (recursive)
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) | |
(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