[racket] Definitions in cond-bodies

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Fri Jun 8 12:08:31 EDT 2012

Yet, the REPL allows this interaction:

> (let ([x 1])
 (cond
   [(= x 1)
    (define u 2)
    (+ u 1)]
   [else u]))
3
> (define (f x) (cond
   [(= x 1)
    (define u 2)
    (+ u 1)]
   [else u]))
> (f 1)
3
> (f 0)
reference to undefined identifier: u

 === context ===
/home/ianj/plt/pltgit/collects/racket/private/misc.rkt:87:7

> (define u 17)
> (f 0)
17

Which seems consistent.
-Ian

----- Original Message -----
From: "Matthias Felleisen" <matthias at ccs.neu.edu>
To: "Sam Tobin-Hochstadt" <samth at ccs.neu.edu>
Cc: "Jens Axel Søgaard" <jensaxel at soegaard.net>, "racket" <users at racket-lang.org>
Sent: Friday, June 8, 2012 12:01:30 PM GMT -05:00 US/Canada Eastern
Subject: Re: [racket] Definitions in cond-bodies


With all due respect, I disagree. Like Jens, I consider the (define u ..) nested and scoped inside the first cond branch. 


On Jun 8, 2012, at 11:56 AM, Sam Tobin-Hochstadt wrote:

> 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
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users


____________________
  Racket Users list:
  http://lists.racket-lang.org/users


Posted on the users mailing list.