When I write code in Dr Racket, I got error message
unsaved-editor:8:2: define: expected only one expression for the function body, but found 3 extra parts in: (define (improve guess x) (average guess (/ x guess)))
But this code can run in Racket or repl.it.
I want to know why error is happening in Dr Racket and is my code really wrong?
My code is this:
(define (average x y) (/ (+ x y) 2))
(define (square x) (* x x))
(define (sqrt1 x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(sqrt-iter 1.0 x))
(sqrt1 9)
Your code is OK for Scheme/Racket. However Student Language is a subset of Scheme, highly limited so it's easier for beginners. It's also used in How To Design Programs book. You can read more about Student Languages (actually there is five of them) on https://docs.racket-lang.org/htdp-langs/index.html.
In case of define
there are important limitations:
define
inside define
).To make your code valid for Student Language, depending on Level (Beginner, Intermediate etc), you can:
letrec*
or local
instead of define
for all local definitionsor
good-enough
, improve
and sqrt-iter
as top level functions.