Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!wiley!scott From: scott@wiley.uucp (Scott Simpson) Newsgroups: comp.lang.lisp Subject: Re: about EVAL Summary: Eval executes in a different environment Keywords: eval, lisp, environment Message-ID: <10090@wiley.UUCP> Date: 8 Jun 90 18:46:34 GMT References: <200.266aae50@wsuiar.uucp> Sender: news@wiley.UUCP Reply-To: scott@wiley.UUCP (Scott Simpson) Organization: TRW Inc., Redondo Beach, CA Lines: 130 In article <200.266aae50@wsuiar.uucp> mnjafar@wsuiar.uucp writes: > I am a newcomer to lisp, so forgive my ignorance. I ran the >test programs shown below on three different machines with >Common Lisp. The results of the tests are shown below. Does >anyone out there know the answer to why Eval behaves in the way >it does. I looked in Guy Steele's book, but did not find an answer, >maybe I don't know where to look . What exactly is supposed to >happen when I issue an (eval '(setq i j)) inside a block ? >;Test1.lsp >(defun strange() > (let (j i xpr) ;j, i and xpr are nil > (setq i 12) > (setq xpr '(setq j i)) > (format t " ~% The value of i inside the function is : ~a~%" i) > (format t " ~% The value of xpr inside the function is : ~a~%" xpr) > (eval xpr) > (format t " ~% The value of j inside the function is :~a~%" j) > ) >) >(strange) Eval is executed in its own local environment. The stack of execution looks like |---------------------| |eval + environment |___ |---------------------| | |strange + environment| | ----------------------- | Can't find i! V Since i is not in eval's environment and it is not in the global environment you get an error. If you change the line (let (j i xpr) to (let (j i xpr) (declare (special i)) then i is dynamically scoped and it will be found in strange's environment. |---------------------| |eval + environment |___ |---------------------| | finds i dynamically, sets global j |strange + environment|<-- ----------------------- The function strange will now completes correctly and returns The value of i inside the function is : 12 The value of xpr inside the function is : (SETQ J I) The value of j inside the function is :NIL Notice that j is nil. The setq in the eval creates and sets a global variable named j (try evaluating j after running the new strange function with the declare). The j inside the function strange is never set. If you wish to set the j inside the function, declare it dynamically scoped also. (let (j i xpr) (declare (special i) (special j)) The result is now The value of i inside the function is : 12 The value of xpr inside the function is : (SETQ J I) The value of j inside the function is :12 No global j is created. >;Test2.lsp >(defun strange() > (let (j i xpr) > (setq i 12) > (setq xpr '(setq j i)) > (format t " ~% The value of i inside the function is : ~a~%" i) > (format t " ~% The value of xpr inside the function is : ~a~%" xpr) > (eval xpr) > (format t " ~% The value of j inside the function is :~a~%" j) > j > ) >) >(setq i 13) >(format t " ~% The value of i outside the function is : ~a~%" i) >(strange) This yielded The value of i inside the function is : 12 The value of xpr inside the function is : (SETQ J I) The value of j inside the function is :NIL J is still nil. The j set by the eval was a global j. A global i exists now though so you only need to make j special. Try making j special and you get The value of i inside the function is : 12 The value of xpr inside the function is : (SETQ J I) The value of j inside the function is :12 > The results obtained >Machine: VAX 8650 running VMS 5.2 > Vax Lisp V 2.2 >Test | Error Message or value of j >--------------------------------------------- >test1 | symbol has no value : I >test2 | nil > >Machine: SUN > Sun Common Lisp, development environment 3.0.2 >Test | Error Message or value of j >--------------------------------------------- >test1 | the symbol I has no global value >test2 | nil These are correct. >Machine: PC/AT compatible > Golden Common Lisp Version 1.01 > >Test | Error Message or value of j >--------------------------------------------- >test1 | 12 >test2 | 12 This is incorrect. Scott Simpson TRW scott@wiley.coyote.trw.com