From ... From: Erik Naggum Subject: Re: [constants] from a reader macro Date: 2000/02/24 Message-ID: <3160367893782736@naggum.no>#1/1 X-Deja-AN: 589200451 References: mail-copies-to: never Content-Type: text/plain; charset=us-ascii X-Complaints-To: newsmaster@eunet.no X-Trace: oslo-nntp.eunet.no 951379757 24476 195.0.192.66 (24 Feb 2000 08:09:17 GMT) Organization: Naggum Software; +47 8800 8879 or +1 510 435 8604; fax: +47 2210 9077; http://www.naggum.no User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.5 Mime-Version: 1.0 NNTP-Posting-Date: 24 Feb 2000 08:09:17 GMT Newsgroups: comp.lang.lisp * Tunc Simsek | Suppose I define a reader macro: | | (defun foo (stream char) | `(make-array 10000 :element-type 'double-float)) | | (set-macro-character #\@ #'foo) I assume this is either a serious confusion or an instructive example whose purpose I don't understand, but to which you can apply answers. | In this silly example each time lisp sees a @ it will return a big array. answering the confusion part: it is important to keep in mind exactly which part of "lisp" sees it under which conditions, and what it returns, not to forget to what it returns it. the Lisp reader will see the @ and return the _list_ it received from foo. if supplied at the top-level, this list is now a form the _evaluation_ of which returns an array, but the Lisp reader is by now long gone, having completed its job. if read as part of some source code, it will only be folded into the source and later processed with that source code. | The question is whether I can make it return the same array each time it | sees it: | | (dotimes (i 10000) | @) as indicated above, this is exactly identical to (dotims (i 10000) (make-array 10000 :element-type 'double-float)) and what happens from then on is not related to the Lisp reader at all, but to the normal behavior of the evaluator and compiler. | should only create one array, i.e. in my opinion, a CONSTANT. however, if you remove the backquote in your misguided reader macro, foo will return a new array that will be treated as a constant by whatever called the Lisp reader each time, but again, the Lisp reader is long gone when this decision is made. it seems by your use of the backquote that your core confusion is to believe that "macro" in "reader macro" is the same kind of "macro" as in "macro function". it isn't. reader macros is a very powerful mechanism to change the syntax of the language, move certain operations into read-time (essentially pre-compile-time), and to abbreviate common forms. reader macros actually make up all the syntax of the language, such that the reader macro for #\( builds lists. I think the reader macro system is absolutely fantastic, but it takes a lot of skill to use it productively, and a lot of serious concern to see when not to use it, just as it takes a lot of intellectual effort to keep syntax simple and clean in general, much more than people generally think -- as witness C++ and Perl, but I'll avoid the digression. #:Erik