eLisp递归函数

时间:2016-03-25 03:15:43

标签: emacs elisp

Lisp新手。尝试将列表传递给递归函数,并且每次都对列表​​中的第一项执行某些操作。这是迄今为止的功能:

(setq colors '(red blue green yellow orange pink purple))

(defun my-function (x)
  (if (> (length x) 0)
      (let ((c  (car x))
            c)
        (my-function x))))

继续收到错误消息,指出x是一个无效元素。不知道该怎么做。

1 个答案:

答案 0 :(得分:4)

如果我重新格式化你的功能,也许你可以看到你做错了什么:

(defun my-function (x)
  (if (> (length x) 0)  ; do nothing if list is empty
      (let ((c (car x)) ; bind c to (car x)
            c)          ; bind c to nil instead
                        ; c is never used
        (my-function x)))) ; recursively call function
                           ; with unmodified x
                           ; until the stack is blown
  

继续收到错误消息,指出x是一个无效元素。

我认为您使用未定义的(my-function x)来调用x,而不是将colors列表与(my-function colors)一起传递,但这肯定不是您唯一的问题。