I just discovered Racket a few days ago, and I\'m trying to get more comfortable with it by writing a little script that generates images to represent source code using #l
Definitions are allowed in a 'body' context, like in lambda
and let
among others. The consequent and alternate clauses of if
are not body contexts; they are expression contexts and therefore definitions are not allowed.
begin
is special - begin
in a body context allows definitions, but begin
in an expression contexts forbids definitions. Your case falls in to the later.
For example:
(define (foo . args) #| body context #|)
(define foo (lambda args #| body context |#))
(define (foo . args)
(let (...)
#| body context |#))
Syntactic keywords that requires expressions: if, cond, case, and, or, when, unless, do, begin
. Check out the formal syntax in any Scheme report (r{4,5,6,7}rs); look for ,
,
, and
.
Also, if you need a body context in an expression, just wrap a let
syntactic form, as such:
(if test
(let ()
(define foo 'foo)
(list foo foo))
alternate)