[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: where define is legal

In article <8904201554.AA13117@spt.entity.com> alms@spt.entity.COM (andrew lm shalit) writes:
>(define (foo bool)
>  (if bool
>      (define (result) #true)
>      (define (result) #false))
>  (result))

 From the R^3S: Section 5.2
"Definitions are valid in some, but not all, contexts where
expressions are allowed. They are valid only at the top level of
a <program> and, in some implementations, at the beginning of
a <body>.

Section 5.2.1 describes top level definitions, and section 5.2.2 describes
internal definitions.

The above code can be rewritten as:
(define result nil) ; dummy value for the variable result.
(define (foo bool)
  (if bool
      (set! result (lambda () #true))
      (set! result (lambda () #false)))

You can use a local variable for result if you want.


p.s. Hi Eric.