I've been studying "A Tour of Scheme in Gambit" and using Android Gambit (AG) to go through some of the exercises. I came across the following interesting lesson and decided to try it. And after I entered it into AG, the system did not come back from the repl.
Gambit v4.6.1
(define (counter)
(let ((value 0)) (lambda () (set! value (+ value 1)) value)))
I tried sending it via the Share functionality to a different e-mail address than my Google address and it did not deliver it, although I could see it in the Gmail Sent folder. I then tried sending it to the Gmail address and that did work fine.
I noticed each time after sharing and then returning to the repl, there was a new prompt; however, I could not get the system to respond. The only thing that worked was killing the AG task and restarting AG.
On a final note, I am mystified as to how the code works. This is an example of it being defined and run on my Windows 7x64 machine:
Gambit v4.6.1
(define (counter)
(let ((value 0)) (lambda () (set! value (+ value 1)) value)))
(define first-counter (counter)) (first-counter)
1
(first-counter)
2
(first-counter)
3
(first-counter)
4
It seems to initialize value to 0 and I assume that happens the first time you use (counter) to define another form. What I don't understand is why the code for initializing is skipped every time (first-counter) is run.
TIA, Steve
Afficher les réponses par date
Take a look at: http://en.wikipedia.org/wiki/Closure_%28computer_science%29
On a final note, I am mystified as to how the code works. This is an example of it being defined and run on my Windows 7x64 machine:
Gambit v4.6.1
(define (counter)
(let ((value 0)) (lambda () (set! value (+ value 1)) value)))
(define first-counter (counter)) (first-counter)
1
(first-counter)
2
(first-counter)
3
(first-counter)
4
It seems to initialize value to 0 and I assume that happens the first time you use (counter) to define another form. What I don't understand is why the code for initializing is skipped every time (first-counter) is run.
TIA, Steve
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
2011/9/15 vasil vasil.s.d@gmail.com:
Take a look at: http://en.wikipedia.org/wiki/Closure_%28computer_science%29
Or here: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-20.html
It is a really fundamental Scheme functionality.
Looks like I introduced a bug in the REPL window code in my last release, passing incomplete multiline expressions to Gambit native code in some cases. I have a fix and will try to put it up on Android Market probably tomorrow. Thanks for finding it.
In the meantime, you should be able to evaluate your definition as a "script" or via telnet into the REPL server, or enter your definition all on one line and evaluate in the REPL window.
"Sharing" contents of the REPL window using an email app can cause Android OS to kill Gambit to free memory, but should restart ok. Try "sharing" a single line version of your expression and see if you have the same problem.
Regards,
Keith
On Sep 14, 2011, at 8:02 PM, Steve Graham wrote:
I've been studying "A Tour of Scheme in Gambit" and using Android Gambit (AG) to go through some of the exercises. I came across the following interesting lesson and decided to try it. And after I entered it into AG, the system did not come back from the repl.
Gambit v4.6.1
(define (counter)
(let ((value 0)) (lambda () (set! value (+ value 1)) value)))
I tried sending it via the Share functionality to a different e-mail address than my Google address and it did not deliver it, although I could see it in the Gmail Sent folder. I then tried sending it to the Gmail address and that did work fine.
I noticed each time after sharing and then returning to the repl, there was a new prompt; however, I could not get the system to respond. The only thing that worked was killing the AG task and restarting AG.
On a final note, I am mystified as to how the code works. This is an example of it being defined and run on my Windows 7x64 machine:
Gambit v4.6.1
(define (counter)
(let ((value 0)) (lambda () (set! value (+ value 1)) value)))
(define first-counter (counter)) (first-counter)
1
(first-counter)
2
(first-counter)
3
(first-counter)
4
It seems to initialize value to 0 and I assume that happens the first time you use (counter) to define another form. What I don't understand is why the code for initializing is skipped every time (first-counter) is run.
TIA, Steve
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
On Wed, Sep 14, 2011 at 08:02:33PM -0700, Steve Graham wrote:
On a final note, I am mystified as to how the code works. This is an example of it being defined and run on my Windows 7x64 machine:
Gambit v4.6.1
(define (counter)
(let ((value 0)) (lambda () (set! value (+ value 1)) value)))
(define first-counter (counter)) (first-counter)
1
(first-counter)
2
(first-counter)
3
(first-counter)
4
It seems to initialize value to 0 and I assume that happens the first time you use (counter) to define another form. What I don't understand is why the code for initializing is skipped every time (first-counter) is run.
To be clear: the identifier "first-counter" is bound to a procedure, defined by lambda statement. The lambda captures the initial value of "counter" when it is defined. But when you call (first-counter), it's not going through the codepath that includes the let statement. It's just executing the lambda code, which has captured the state of "counter", and that state persists as long as its enclosing lambda does (the lambda "closes over" counter; that's why it's called a "closure").
You can think of counter as a private data member in an OO Algol language.
-Joe