我试图通过创建一个函数来更好地理解 lisp 中的多级列表,在该函数中我从多级列表中删除所有不是数字的元素。我首先尝试使用 striv0 函数将其展平,然后检查列表的第一个参数是否为数字。但是我在尝试该功能时遇到问题
(函数'(5 (5 2 (8)(7 (9)5))))
.我得到 :COND :variable IF HAS NO VALUE
代码:
(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)))))))
我相信我已经通过检查数字参数或内部列表(如果两者都不是的话)将第二个函数融入第一个函数来解决问题:
(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)) )
)
)
)
最佳答案
这里有一些逐行的反馈和一些提示:
(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?
关于LISP 更好理解多级列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53034319/