Subject: Re: [constants] from a reader macro
From: Erik Naggum <erik@naggum.no>
Date: 2000/02/24
Newsgroups: comp.lang.lisp
Message-ID: <3160367893782736@naggum.no>

* Tunc Simsek <simsek@tudor.EECS.Berkeley.EDU>
| 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