Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!accuvax.nwu.edu!krulwich From: krulwich@ils.nwu.edu (Bruce Krulwich) Newsgroups: comp.lang.scheme Subject: Re: Non-local variables Message-ID: <7478@accuvax.nwu.edu> Date: 9 May 90 17:40:10 GMT References: <9005081621.AA13048@mtecv2.mty.itesm.mx> <1990May8.205719.2014@sun.soe.clarkson.edu> Sender: news@accuvax.nwu.edu Reply-To: krulwich@ils.nwu.edu (Bruce Krulwich) Organization: Institute for the Learning Sciences, Northwestern University, Evanston, IL 60201 Lines: 38 In-reply-to: jk0@image.soe.clarkson.edu (Jason Coughlin) In article <1990May8.205719.2014@sun.soe.clarkson.edu>, jk0@image (Jason Coughlin) writes: > Hi. I've got a short little example which creates a non-local >variable to implement a simple little counter. In my implementation of >Scheme, I create it like this: > >(let ([a 0]) > (define counter > (lambda () > (set! a (+ a 1)) > a > ) > ) >) > > PC-Scheme barfs on this complaining about an illegal letrec syntax. I think that the problem is that DEFINE's that are not at the top-level are considered by Scheme (but not T or CL) to be local definitions like LETREC's. Probably the code you give above is transformed into a LET containing a body-less LETREC, which is illegal. This is an issue that has been discussed before. Basically, the treatment of internal DEFINE's as local definitions makes it impossible to have static locally-scoped variables that span across procedures. (Having static variables for one procedure can be achieved as you showed later in your message.) Having internal DEFINE's result in local definitions doesn't add any power, because they're identical in meaning to LETREC, but to some people this seems more semantically correct than simply having all DEFINE's result in top-level definitions. Of course, another issue for many people is that changing the meaning would make old code incorrect. Bruce Krulwich Institute for the Learning Sciences krulwich@ils.nwu.edu