我试图通过创建一个函数来更好地理解lisp中的多级列表,在该函数中我从多级列表中删除所有不是数字的元素.我首先尝试使用striv0函数展平它,然后检查列表的第一个参数是否为数字.但是我在尝试函数时遇到问题
(func'(5(5 2(8)(7(9)5)))).我得到:COND:变量如果没有价值
代码 :
(DEFUN striv0 (lis)
(COND ((NULL lis) NIL)
((ATOM lis) (LIST lis))
(T (APPEND (striv0 (FIRST lis))
(striv0 (REST lis))))
))
(DEFUN func (lis)
(LET( (newList (striv0 lis)) ))
(COND ((NULL newList) NIL)
( T(IF (NUMBERP (FIRST newList))
((func(REST newList)))))))
Run Code Online (Sandbox Code Playgroud)
我相信我已经设法通过检查数字参数或内部列表将第二个函数模拟到第一个函数来解决问题,如果它不是它在列表中进一步移动:
(DEFUN checkNumber (lis)
(COND ((NULL lis) NIL)
((ATOM lis) (LIST lis))
(T (if ( or(NUMBERP ( FIRST lis))(LISTP (FIRST lis)))
(APPEND ( checkNumber (FIRST lis))
( checkNumber (REST lis)))
( checkNumber (REST lis)) )
)
)
)
Run Code Online (Sandbox Code Playgroud)
这里有一些逐行反馈和一些提示:
(DEFUN func (lis) ; why is this called FUNC
; and not something useful?
; why is the variable called
; LIS and not LIST?
(LET( (newList (striv0 lis)) )) ; why is this not indented?
; why does LET not have
; body forms?
; why is the LET ending here?
(COND ((NULL newList) NIL) ; why is this not indented?
( T(IF (NUMBERP (FIRST newList)) ; why is this not indented?
((func(REST newList))))))) ; why are there so
; many parentheses?
; why is it starting
; with two parentheses?
Run Code Online (Sandbox Code Playgroud)