Trying out exercises from "Structure and Interpretation of Computer Programs" second edition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
708 B

#! /usr/bin/racket
#lang racket/base
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* (term a) result))))
(iter a 1))
(define (numerator pre-applying n)
(* pre-applying (product (lambda (x) (* x x)) 4 (lambda (x) (+ x 2)) n)))
(define (denominator pre-applying n)
(* pre-applying (product (lambda (x) (* x x)) 3 (lambda (x) (+ x 2)) n)))
(define (fourth-of-pi n)
(cond ((< n 1) 0)
((even? n) (/ (numerator (* 2 (+ n 2)) n)
(denominator 1 ( + n 1))))
(else (/ (numerator 2 (+ n 1))
(denominator (+ n 2) n)))))
(fourth-of-pi 1)
(fourth-of-pi 2)
(fourth-of-pi 3)
(fourth-of-pi 60)