common lisp: how can a macro define other methods/macros with programmatically generated names?

别说谁变了你拦得住时间么 提交于 2019-12-03 10:48:31

Try this:

(defmacro def-trio (base-name)                                         ; changes:
  (let*                                                                ; 3.
    ((package (symbol-package base-name))                              ; 2.
     (helper-name (intern (format nil "HELPER-~a" base-name) package)) ; 1. 4.
     (method-1 (intern (format nil "~a-1" base-name) package))         ; 1.
     (method-2 (intern (format nil "~a-2" base-name) package)) )       ; 1.
    `(progn 
          (defun ,helper-name () 'helper-called)
          (defun ,method-1 () (,helper-name) '1-called)
          (defun ,method-2 () (,helper-name) '2-called) )))

The following changes were made to your original definition -- the first change is the crucial one:

  1. Interned each of computed symbol names into the same package as the base name using (intern ... package).
  2. Introduced the variable package which is bound to the package of the supplied base-name symbol.
  3. Changed let to let* to allow the newly introduced variable package to be referenced in subsequent variables.
  4. Changed the prefix of the helper method to upper case to match the convention for normal Lisp symbols.

Use INTERN to turn the generated function name strings into symbols.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!