代码之家  ›  专栏  ›  技术社区  ›  jppower175

eLisp递归函数

  •  0
  • jppower175  · 技术社区  · 9 年前

    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是一个void元素。不知道该怎么办。

    1 回复  |  直到 9 年前
        1
  •  4
  •   phils    9 年前

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

    (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是一个void元素。

    我想你在打电话 (my-function x) 具有未定义的 x 而不是把它传给你 colors 与列出 (my-function colors) 但这肯定不是你唯一的问题。