LISP 更好理解多级列表

标签 lisp common-lisp

我试图通过创建一个函数来更好地理解 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/

相关文章:

Clojure:将值设置为键

scheme - 有人可以解释 : (+2 (if (>b a) b a))?

lisp - 在 Common Lisp 的列表中查找嵌套最多的列表

common-lisp - Common Lisp 中函数的作用域

lisp - Common Lisp 宏和 Forth 元编程能力的比较

design-patterns - lisp:使用两个相互依赖的参数正确调用一个函数

user-interface - 是否有用于 Common Lisp 的高级、易于安装的 GUI 库?

lisp - 元素未添加到列表

common-lisp - 为什么 Common Lisp (SBCL) 会为一个简单的程序使用这么多内存?

lisp - 与密码作斗争