(defun for-loop (op list-var)
"对list-var中的元素,调用op"
(if (not (eq list-var nil))
(progn
(funcall op (car list-var))
(for-loop op (cdr list-var)))))
(defun append-to-list (list-var element-list)
(let ((op (lambda (item) (add-to-list list-var item))))
(for-loop op element-list)))
为了方便操纵列表,我在init.el中写了上面两个函数,调用时:
(setq list-1 '(1 2 3))
(append-to-list 'list-1 '(4 5 6))
但是emacs启动时,会报错,栈信息如下:
Debugger entered--Lisp error: (wrong-type-argument symbolp (4 5 6))
add-to-list((4 5 6) 4)
(lambda (item) (add-to-list list-var item))(4)
funcall((lambda (item) (add-to-list list-var item)) 4)
(progn (funcall op (car list-var)) (for-loop op (cdr list-var)))
(if (not (eq list-var nil)) (progn (funcall op (car list-var)) (for-loop op (cdr list-var))))
for-loop((lambda (item) (add-to-list list-var item)) (4 5 6))
(let ((op (function (lambda (item) (add-to-list list-var item))))) (for-loop op element-list))
append-to-list(list-1 (4 5 6))
只有当我把for-loop中的参数list-var换成别的,比如lst-var,就正常了,有没有人了解这是为什么?
看上去是for-loop和append-to-list中使用了同名参数list-var造成的,但是理论上不应该啊,两个函数没有什么关联,参数应该是局部环境的吧?