[racket] Definitions in cond-bodies

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Fri Jun 8 11:56:31 EDT 2012

On Fri, Jun 8, 2012 at 11:42 AM, Jens Axel Søgaard
<jensaxel at soegaard.net> wrote:
>
> When
>
> (let ([x 1])
>  (cond
>    [(= x 1)
>     (define u 2)
>     (+ u 1)]
>    [else u]))
>
> is pasted into the interaction window, I get 3  !
>
> I was expecting an error.

This is the same behavior as:

-> (if #f x 1)
1

What's happening is that `#%top` behaves differently at the REPL than
in a module.  This enables you to write:

-> (define (even? x) (or (zero? x) (odd? (sub1 x))))
-> (define (odd? x) (or (= x 1) (even? (sub1 x))))

without getting an error when entering the first line.
-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.