Subject: Re: Top-level closures in scheme?
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 11 Sep 2001 20:04:28 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9nlqoc$iblgi$1@fido.engr.sgi.com>
Jeffrey Siegal  <jbs@quiotix.com> wrote:
+---------------
| brlewis@my-deja.com wrote:
| \> (define somefuncs
| >   (let ((somevar 3))
| >     (define (incvar n)
| >       (set! somevar (+ somevar n)))
| >     (define (what-is-it?)
| >       (cons 'x somevar))
| >     (cons incvar what-is-it?)))
| > 
| > (define incvar (car somefuncs))
| > (define what-is-it? (cdr somefuncs))
| > 
| > I have my own ideas about how to make this code more elegant.  Others
| > have theirs.  But it's perfectly good code as-is.  I don't consider one
| > wasted cons cell at load time any kind of bug.
| 
| The cons cell is not a bug but the top-level namespace pollution is ugly.
+---------------

Which is why (if you don't want to use "define-values") the "standard"
Scheme idiom not the above, but this one:

	(define incvar #f)
	(define what-is-it? #f)
	(let ((somevar 3))
	  (set! incvar (lambda (n) (set! somevar (+ somevar n))))
	  (set! what-is-it? (lambda () (cons 'x somevar))))


-Rob

-----
Rob Warnock, 30-3-510		<rpw3@sgi.com>
SGI Network Engineering		<http://reality.sgi.com/rpw3/>
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA

[Note: aaanalyst@sgi.com and zedwatch@sgi.com aren't for humans ]