what is to append as push is to cons, in Lisp?

前端 未结 5 1356
太阳男子
太阳男子 2020-12-11 03:15
(push x list)

expands to

(setq list (cons x list))

What expands to the following:

(setq list (app         


        
5条回答
  •  情书的邮戳
    2020-12-11 03:37

    Joshua Taylor mentioned how to do it in Common Lisp. I will answer how in Emacs Lisp:

    (require 'cl-lib)
    (defmacro appendf (place &rest lists)
      `(cl-callf append ,place ,@lists))
    (defmacro prependf (list place)
      `(cl-callf2 append ,list ,place))
    

    And some test:

    (let ((to-prepend '(the good))
          (acc '(the bad))
          (to-append-1 '(the weird))
          (to-append-2 '(pew pew)))
      (prependf to-prepend acc)
      (appendf acc to-append-1 to-append-2)
      (list :acc acc
            :to-prepend to-prepend
            :to-append-1 to-append-1
            :to-append-2 to-append-2))
    ; ⇒ (:acc (the good the bad the weird pew pew) :to-prepend (the good) :to-append-1 (the weird) :to-append-2 (pew pew))
    

    Macro expansion test:

    (let ((print-gensym t))
      (print
       (macroexpand '(prependf y (cddr x)))))
    ; prints (let* ((#:a1 y) (#:v x)) (setcdr (cdr #:v) (append #:a1 (cddr #:v))))
    

    For macroexpand-1 and pretty printing, use the macrostep package.

提交回复
热议问题