solutions/Structure and Interpretatio.../sicp-ex-1.31.rkt

28 lines
708 B
Racket
Raw Permalink Normal View History

2024-04-16 19:13:00 -04:00
#! /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)