Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mcvax!cernvax!ethz!ceb From: ceb@ethz.UUCP (Charles Buckley) Newsgroups: comp.lang.lisp Subject: Re: A small brush fire ... Message-ID: <412@ethz.UUCP> Date: Sat, 29-Nov-86 22:05:16 EST Article-I.D.: ethz.412 Posted: Sat Nov 29 22:05:16 1986 Date-Received: Mon, 24-Nov-86 22:36:45 EST Organization: CS Department, ETH Zuerich, Switzerland Lines: 85 In his posting of 21 November (our number 535 - the numbering schemes seem to play a time zone style game as the messages cross the Atlantic), Mr. Steele responds to Mr. Sloan's challenge (recalled here) > So how would you answer the following: Since "good" >programmers know better than using free (== global) variables, >the argument between lexical and dynamic scoping is really a >mute point. The real issue this: Can you exhibit a piece of code >that depends on the scoping rules that can't be coded AS WELL >in a way that doesn't depend on the scoping rules? > with the following redefinition of mapcar, and an application function using it: (defun mymapcar (myfunction list) (cond ((null list) nil) (t (cons (myfunction (car list)) ;or funcall, etc. (mymapcar myfunction (cdr list)))))) (defun consall (l list) (mymapcar '(lambda (x) (cons x list)) l)) lexical: (consall '(a b c) '(d)) ==> ((a d) (b d) (c d)) dynamic: (consall '(a b c) '(d)) ==> ((a a b c) (b b c) (c c)) The code presented certainly depends on the scoping rules, but the second part of the challenge remains unanswered, i. e. "can't be coded AS WELL in a way that doesn't depend on the scoping rules." In fact, in the context of the (perforce) interpreted lambda-expression which makes up the function pased as an argument, list is a special variable which must be resolved by name at runtime, an expensive process. Therefore, many would not consider the above an example of good code at all. One rule independent way of coding the above (I use Zetalisp syntax) is (defun mynewmapcar (function list-to-be-indexed &rest fixed-args) (cond ((null list-to-be-indexed) nil) (t (cons (lexpr-funcall function (car list-to-be-indexed) fixed-args) (lexpr-funcall 'mynewmapcar function (cdr list-to-be-indexed) fixed-args))))) (defun consall (l list) (mynewmapcar 'cons l list)) [lexpr-funcall is the same as funcall, except the last argument is used to terminate the argument list passed to the funcalled function, instead of being the last element in a nil terminated list.] The above behaves in the manner implied by Mr. Steele's example regardless of scoping rules, and supports Mr. Sloan's point about free (special) variables. Note I called the workhorse function 'new' mapcar, because the issue lurking behind al of this is that if the meta-functions which we have grown accustomed to were semantically more powerful, the brush fire would have remained as someones smoldering cigarette ashes. Dr. Charles E. Buckley uucp: mcvax!ethz!jungfrau!ceb earn: BUCKLEY@CZHETH5A Institut fuer Integrierte Systeme ETH-Zentrum CH-8092 Zuerich (Suisse) Tel: 01/256 5245 (national) +411 256 5245 (international) -- Dr. Charles E. Buckley uucp: mcvax!ethz!jungfrau!ceb earn: BUCKLEY@CZHETH5A Institut fuer Integrierte Systeme ETH-Zentrum CH-8092 Zuerich (Suisse) Tel: 01/256 5245 (national) +411 256 5245 (international)