Subject: Re: Top-level closures in scheme?
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 10 Sep 2001 09:32:58 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9ni1ca$i0hb8$1@fido.engr.sgi.com>
Matthias Radestock  <matthias@sorted.org> wrote:
+---------------
| > argamon@sunlightNOSPAM.cs.biu.ac.il (Dr. Shlomo Argamon ) writes:
| > > Hi.  I'm returning to the Lisp world after a (too long!) hiatus, and
| > > am trying out scheme instead of my previous beloved Common Lisp.
| > > Anyway, scheme has this concept of "internal definitions" which seem
| > > to make it difficult to create "top-level closures", which I'd do in
| > > CL as:
| > >
| > > (let ((somevar 3))
| > >   (defun incvar (n)
| > >     (incr somevar n))
| > >   (defun what-is-it? ()
| > >     (cons 'x somevar)))
| 
| In Schemes that support DEFINE-VALUES, it should be possible to do this
| more concisely (and staying somewhat closer to the CL equivalent):
| 
| (define-values (incvar what-is-it?)
|   (let ((somevar 3))
|     (define (incvar n) (set! somevar (+ somevar n)))
|     (define (what-is-it?) (cons 'x somevar))
|     (values incvar what-is-it?)))
+---------------

Exactly. That works fine, for example, in MzScheme:

	> (what-is-it?)
	(x . 3)
	> (incvar 5)
	> (what-is-it?)
	(x . 8)
	> 

Though some might prefer to avoid the name overloading implied by the
internal defines, and just use simple anonymous lambda expressions:

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

By the way, why the "(cons 'x somevar)" for the value accessor?
Why not just "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 ]