[Note: Newsgroups trimmed to something reasonable.]
Sady <treinish@bgumail.bgu.ac.il> wrote:
+---------------
| this is what i tried:
| (define (flip)
| (let ((even-or-odd 0))
| (lambda (even-or-odd)
| (if (odd? even-or-odd)
| (set! even-or-odd 0)
| (set! even-or-odd 1)))
| even-or-odd))
+---------------
There are several things wrong here (much of which, though not all,
could have been avoided by proper indenting)...
I think you want a closure to *be* the value of "flip", not be an
unused do-nothing value. As written, your code is equivalent to this:
(define (flip)
(let ((even-or-odd 0))
; the lambda value was unused
even-or-odd))
so of course it was always going to return zero.
Or to look at it another way, here's what you actually wrote when the
short-hand "define" syntax is expanded and it's indented to match the
nesting of the parens:
(define flip
(lambda ()
(let ((even-or-odd 0))
(lambda (even-or-odd) ; this procedure value is never used
(if (odd? even-or-odd)
(set! even-or-odd 0)
(set! even-or-odd 1)))
even-or-odd)))
Hints: The last "even-or-odd" is in the wrong place; so is the "let";
and you don't need the second "lambda" at all. (The formal arg of the
inner lambda shadows the "let" variable, so you could never access it
anyway.) Hopefully that will be enough for you to make progress...
-Rob
-----
Rob Warnock, 41L-955 rpw3@sgi.com
Applied Networking http://reality.sgi.com/rpw3/
Silicon Graphics, Inc. Phone: 650-933-1673
1600 Amphitheatre Pkwy. PP-ASEL-IA
Mountain View, CA 94043