Search the Community
Showing results for tags 'abelson'.
-
Good evening, everyone. I've recently started to watch the Abelson-Sussman MIT lectures on computer science (mostly because I find them very interesting, not because they have much to do with what I do) and I've ran across this piece of code to create a counter: (define make-counter (lambda (N) (lambda () (set! N (1+ N)) N))) (define c1 (make-counter 1)) (define c2 (make-counter 10)) The explanation concerning the mentioned code starts at around the 48 minute mark. The syntax is not that of AutoLISP and neither that of Common Lisp, I believe, mostly because it's a fragment taken from the middle of a lecture and there's a good reason for the way the code above is written. Anyway, what is supposed to happen is that when you run (c1) for the first time you get a 2; for the second time you get a 3 etc.; when you run (c2) you get (regardless of when you run (c1)) 11; the next time you get 12 etc. I attempted to translate it to AutoLISP: (defun make-counter (N) (lambda () (setq N (1+ N)) N) ) (defun c1 () ((make-counter 1)) ) (defun c2 () ((make-counter 10)) ) The problem is that this does not work. When I attempt to evaluate this with N being nil I get an error. If N is preset to some integer/real then it will be incremented, regardless of calling c1 or c2. I'd like to know if I'm doing something wrong or if this is something that I simply cannot do in AutoLISP. Can this piece of code run the way Sussman shows it in his lecture? Is there a catch here? This is not a matter of life or death, obviously, and I'm not interested in another piece of code that works as a counter (though I'd be delighted to see whichever ones you guys came up withd) since this is something I can easily get around. All I wanted is to know if this can work in AutoLISP and, if so, how. Thank you all!
- 3 replies
-
- mit lecture
- make-counter
-
(and 3 more)
Tagged with: