Currently going through SICP, and near the end of the first chapter, they ask you to program a value for pi, with
pi/4 = (2 * 4 * 4 * 6 * 6 * 8 * ...) / (3 * 3 * 5 * 5 * 7 * 7 *..)
I have the following functions defined:
;Term and Next are both functions, a and b are the range of the product
(define (product term a next b)
(if (> a b) 1
(* (term a) (product term (next a) next b))))
and
(define (pi-approx n)
(define (square x) (* x x))
(define (num-prod ind) (* (* 2 ind) (* 2 (+ ind 1)))) ; calculates the product in the numerator for a certain term
(define (denom-prod ind) (square (+ (* ind 2 ) 1))) ;Denominator product at index ind
(define num (product num-prod 1 inc n))
(define denom (product denom-prod 1 inc n))
(* 4 (/ num denom))) ;;Resulting value
When I run this code in DrRacket, I get the following error:
num-prod: Undefined; Cannot use before initialization
, even though I initialize num-prod a couple of lines before I use it.
What am I doing wrong syntactically?