尝试在 elisp 中定义递归宏
(defmacro remacro (keys)
(if keys
`(func1 ,(car keys)
,(remacro (cdr keys)))
))
(macroexpand '(remacro '(a b c)))
但是它结束于
Lisp nesting exceeds `max-lisp-eval-depth'
错误。
想要得到这样的结果
(func1 a (func1 b (func1 c nil nil) '(c)) '(b c))
来自
(remacro '(a b c))
请告诉我如何更正此定义。
还有一件事我可以将 `keys' 定义为 rest 参数,例如
(defmacro remacro (&rest keys)
(if keys
`(abc ,(car keys)
,(remacro `,@(cdr keys)))
))
试过这个,但没用。
用例:
基本上我想定义一个函数
设置一个以列表方式排列的树节点
(它仍然没有工作,必须继续努力)
(defmacro set-tree-node (tree e &rest keys)
`(setcdr
,(if keys
`(assoc (car keys)
(pushnew
(list ,(car keys))
(cdr
,(set-tree-node `(cdr ,xtree) e `,@(cdr keys)))
:key 'car))
tree)
e))
(setq egtree nil)
运行后
(set-tree-node egtree new-node n b c)
应该得到
树均衡
((n (b (c . new-node))))
或
(n (b (c . new-node)))
我把它定义为函数
(defun set-tree-node (tree e &rest keys)
(setcdr
(reduce (lambda (xtree k)
(message "tree %s k %s" xtree k)
(assoc k (pushnew (list k) (cdr xtree) :key 'car)))
keys :initial-value (cons nil tree))
e))
但它只适用于现有列表
它可以成功更改(如果存在完整路径则树)
egtree from
(setq egtree '((n (b (c . d)))))
到
egtree eq
'((n (b (c . replaced-d))))
这样调用之后
(set-tree-node jt 'replaced-d 'n 'b 'c)
但如果完整路径不可用,则此函数不适用于 if list 导出
最佳答案
将宏写成:
(defmacro remacro (keys)
(if keys
`(abc ,(car keys)
(remacro ,(cdr keys)))))
并调用它:
(remacro (a b c))
您不需要引用参数,因为不会评估宏参数。
要查看扩展,请使用:
(macroexpand-all '(remacro (a b c)))
(abc a (abc b (abc c nil)))
在您的示例中,我没有看到 add
应该来自哪里,我认为这是 abc
的拼写错误。
关于macros - Elisp递归宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16510603/