[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Weird scope of variable defined inside cond
From: |
Mario Domenech Goulart |
Subject: |
Re: [Chicken-users] Weird scope of variable defined inside cond |
Date: |
Tue, 12 May 2015 12:18:38 +0000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) |
Hi Vitor,
On Tue, 12 May 2015 02:44:30 -0300 Vítor De Araújo <address@hidden> wrote:
> I'm seeing some weird behavior regarding scope of a variable defined
> with 'define' inside a cond clause. For instance:
>
> (define (foo x)
> (define bar 23)
> (cond [(number? x)
> (define baz 42)
> (list baz)]
> [else 'whatever]))
>
> (foo 3)
> (print baz) ; prints 42!
> (print bar) ; unbound variable, as expected
>
> Additionally, if I type the above definition of 'foo' directly in csi,
> I get a message:
>
> Note: the following toplevel variables are referenced but unbound:
>
> baz (in foo)
>
> Is this a bug, or I'm missing something? I'm running Chicken 4.9.0.1 on
> Debian GNU/Linux x86-64. I also tried it with the version from the git
> repository and got the same result.
That's a little can of worms. :-)
The definition of `baz' is not valid in that context according to R5RS,
since `define' forms are not expressions (they are definitions), and
`cond' clauses should contain expressions:
http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-7.html#%_sec_4.2.1
CHICKEN implements `define' with `set!', and `set!' in CHICKEN can be
applied to unbound variables, in that case creating toplevel ones:
http://wiki.call-cc.org/man/4/Extensions%20to%20the%20standard#set
So, that code ends up creating a toplevel binding for `baz':
$ csc -debug 2 foo.scm
[canonicalized]
(##core#callunit "library")
(##core#callunit "eval")
(##core#callunit "chicken_2dsyntax")
(##core#undefined)
(set! foo
(##core#lambda
(x1)
(let ((bar6 (##core#undefined)))
(let ((t9 (set! bar6 '23)))
(if (number? x1) (let ((t8 (set! baz '42))) (list baz)) 'whatever)))))
(foo '3)
(print baz)
((##sys#implicit-exit-handler))
(##core#undefined)
Best wishes.
Mario
--
http://parenteses.org/mario