Subject: Re: Simple question on closures
From: (Rob Warnock)
Date: 1997/01/22
Newsgroups: comp.lang.scheme
Message-ID: <5c48jm$>

John L. Stein <> wrote:
| STk> (define (bob y)
|     (lambda (x) (write (+ x y)) ))
| #[undefined]
| STk> (define x 7)
| #[undefined]
| STk> (bob 4)
| #[closure 202748c0]
| STk> 
| Should'nt (bob 4) yield 11 ? Does'nt (bob 4) create the
| closure with x defined as 7.  Guess I don't understand closures.

Actually, it's probably "define" you're having trouble with...

Let's rewrite what you wrote without using the optional/alternative
define syntax (which seems to be confusing things here):

	(define bob (lambda (y) (lambda (x) (write (+ x y)))))

That's exactly the same as what you wrote. Really. In either case,
"bob" is a procedure that returns a closure over its argument.
Said closure writes *its* argument plus whatever bob's argument was:

	> (define bob4 (bob 4))
	> bob4
	> (bob4 3)

[Might have been more useful not to do a "write" in this case, but
to just return the sum, hmmm?]


p.s. Remaining exercise for the student: Why didn't "(define x 7)"
do anything? [Hint: Read the chapter on "lexical scope".]

Rob Warnock, 7L-551
Silicon Graphics, Inc.
2011 N. Shoreline Blvd.		Phone: 415-933-1673  FAX: 415-933-0979
Mountain View, CA  94043	PP-ASEL-IA