David Shaked
David Shaked

Reputation: 3381

Let Over Lambda: symbolic representation in macros

Doug Hoyte defines in his introduction to Let Over Lambda the symb function as an essential utility to meta-programming with macros:

In clisp:

(defun mkstr (&rest args)
  (with-output-to-string (s)
    (dolist (a args) (princ a s))))

(defun symb (&rest args)
  (values (intern (apply #'mkstr args))))

Why are symbols so important in macro programming?

Upvotes: 0

Views: 106

Answers (2)

sds
sds

Reputation: 60062

Symbols name functions and macros, so if your macro defines functions, it will have to construct symbols.

E.g., when you do (defstruct foo a b c), you are defining these functions:

  1. MAKE-FOO
  2. FOO-P
  3. COPY-FOO
  4. FOO-A
  5. FOO-B
  6. FOO-C
  7. (SETF FOO-A)
  8. (SETF FOO-B)
  9. (SETF FOO-C)

which requires constructing the 6 symbols above.

PS. You might find the output of (macroexpand-1 (defstruct foo a b c)) instructive.

Upvotes: 4

user5920214
user5920214

Reputation:

Symbols are how things are named in Lisp, and macros often want to talk about names.

Imagine you want to write your own version of defstruct: well a form like

(defstruct pos x y)

is going to need to create names -- symbols -- like pos-x, make-pos, pos-p and so on. So you need ways of talking about symbols: creating symbols from other ones, creating secret symbols which only you know about and which are different than any other symbols and so on.

I kind of disagree that the symb function he defines is essential, but you do need to be able to manipulate symbols.

Upvotes: 2

Related Questions