Quote from SICP:

Internal definitions should be at the beginning of the procedure body. For the consequences of launching programs that mix definitions and their use, the administration is not responsible.

An example of the correct code

(define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0)) 

Does this mean that I can't do this? -

 (define (test x) (define (test2) (* xx)) (test2) (define (test3) (+ xx)) (test3)) 

Or is it implied that I should declare it before use?

    1 answer 1

    The standard language Scheme - R5RS ( here ) is written the same way. I ran this code in the Chez Scheme and got:

     Exception: invalid context for definition (define (test3) (+ xx)) 

    EDIT:

    There is a counterexample. I ran the same code in Chibi Scheme (R7RS) and now it worked correctly. The same result in Racket. But the order of definitions does matter. When I launched:

     (define (test x) (test1) (define (test1) (* xx))) 

    received:

     ERROR in test: undefined variable: test1 Searching for modules exporting test1 ... 
    • Do you have a second example? - ZX-SPECTRUM