[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Weird scope of variable defined inside cond
From: |
Vítor De Araújo |
Subject: |
Re: [Chicken-users] Weird scope of variable defined inside cond |
Date: |
Tue, 12 May 2015 10:44:44 -0300 |
Thanks for the replies! I found it strange because I was under the
impression that in Chicken you generally can define variables in places
not permitted by R5RS. For instance:
(define (f)
(print 23)
(define x 42)) ; definition after expressions,
; which I think is non-standard
(f) ; prints 23
(print x) ; unbound variable
Anyway, for the program I had this problem with I redefined 'cond' to
wrap the clause body in a (let () ...) and everything seems to work.
(By the way, I didn't know about "-debug 2", which is something I
wanted before. :D)
On 12/05/2015 08:12 -0400, Mario Domenech Goulart wrote:
> 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
>
>
--
Vítor De Araújo
http://inf.ufrgs.br/~vbuaraujo