SICP exercise 4.5
This page is a followup of this.
form http://paste.lisp.org/display/48776.
Our goal is this.
(define result 1)
(cond ((assoc 'b '((b 2))) => (lambda (x) (+ result (cadr x))))
(else false))
;; evaluates to 3
My evaluator expands the expression to this.
(let ((result (assoc 'b '((b 2))))) (if result ((lambda (x) (+ result (cadr x))) result) false))
When the environment has a variable named result, it encounts a name collision.
I can escape the problem easily like this.
(let ((result_ (assoc 'b '((b 2))))) ; change result to result_
(if result
((lambda (x) (+ result (cadr x))) result_)
false))
It runs correctly only in the situation. However this is not the solution that he want.
The GYNSYM system is a good idea.
My solution is this.
I introduce a reserved word like *COND->IF_RESULT* in my system.
« コミュニケーション | Main | 動画の力 »
The comments to this entry are closed.


Comments
Hi
I asked about this on IRC and received this insightful reply from zbigniew:
"yes, that exercise exactly illustrates the variable capture problem encountered in non-hygienic macros. What is important is that you noticed it. If you want to fix it, use your implementation's version of gensym or come up with a symbol which is unlikely to clash."
http://ircbrowse.com/channel/scheme/20071007#hour18
Posted by: nicolasv | 2007.10.08 09:45 AM
Hi, nicolasv.
Thank you for your replay.
I can understand what he say.
SICP is one of the best book I have read.
Enjoy yourself!
Posted by: Natsutan | 2007.10.08 03:38 PM