[plt-scheme] An odd bug with define-values

From: Akiva Kleiman (kela_bit at netvision.net.il)
Date: Wed Mar 5 12:11:19 EST 2003

Check these two code bits out:

(define-values (x y)
  (values 3 x))
(display x)
(newline)
(display y)

would return an error - undefined variable x. That's logical. But...

(define-values (x y)
  (let ([x '*]
        [y '*])
    (define-values (x y)
      (values 3 x))
    (values x y)))
(display "x = ") (display x)
(newline)
(display "y = ") (display y)

would return:
x = 3
y = #<undefined>

although it *should* return
x = 3
y = *

Oops.

The problem, I believe is that define-values prepares a location for the new 
variable before evaluating. Thus, when trying to refer to an outer varaiable 
with the same name, you actually refer to the inner variable. Which is still 
#<undefined>, of course.

Katsmall the Wise
kela_bit at netvision.net.il




Posted on the users mailing list.