John L. Stein <stein@seas.ucla.edu> 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
#<procedure>
> (bob4 3)
7>
See?___/
[Might have been more useful not to do a "write" in this case, but
to just return the sum, hmmm?]
-Rob
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 rpw3@sgi.com
Silicon Graphics, Inc. http://reality.sgi.com/rpw3/
2011 N. Shoreline Blvd. Phone: 415-933-1673 FAX: 415-933-0979
Mountain View, CA 94043 PP-ASEL-IA