Recursion 公共Lisp:递归函数返回nil

Recursion 公共Lisp:递归函数返回nil,recursion,lisp,Recursion,Lisp,我试图编写一个递归返回列表负值的函数。这就是我到目前为止所做的: (defun neg-nums (L) (if (null L) nil (let ((X (neg-nums (cdr L)))) (if (< (car L) 0) (append (list (car L)) X))))) (print (neg-nums (list 1 -2 3 -4))) (除负整数(L) (如果(空L)无 (让((X(负整数)(cdr L))) (如果(

我试图编写一个递归返回列表负值的函数。这就是我到目前为止所做的:

(defun neg-nums (L)
(if (null L) nil
  (let ((X (neg-nums (cdr L))))
         (if (< (car L) 0) (append (list (car L)) X)))))

(print (neg-nums (list 1 -2 3 -4)))
(除负整数(L)
(如果(空L)无
(让((X(负整数)(cdr L)))
(如果(<(左车)0)(附加(列表(左车))X()())))
(打印(表1-23-4))
我在递归之外尝试了let函数,它工作得很好,所以我知道这与尝试使它递归有关。现在,它只在应该输出(-2-4)的时候输出NIL

(PS,打印功能是这样的,当我将其作为文件加载时,我可以立即看到结果。)


任何帮助都将不胜感激。

抱歉,我刚刚意识到我需要在最后一行多加一个X。正确答案是:

(defun neg-nums (L)
(if (null L) nil
  (let ((X (neg-nums (cdr L))))
         (if (< (car L) 0) (append (list (car L)) X) X))))

(print (neg-nums (list 1 -2 3 -4)))
(除负整数(L)
(如果(空L)无
(让((X(负整数)(cdr L)))
(如果(<(左车)0)(附加(列表(左车))X)X)))
(打印(表1-23-4))