小编Nor*_*rby的帖子

LISP确定列表的深度

我正在尝试编写一个确定列表深度的函数。因此对于

  • (1 2 3 4)=> 1
  • (1 2 3(4))=> 2
  • (1 2 3(4(5)))=> 3

等等。

这是我到目前为止编写的内容,它仅适用于线性列表(深度= 1)和深度为2的列表。

我认为我已经接近正确的解决方案,但我觉得自己缺少一些东西。

(defun detlen (lst count)
    (
        cond
            ((null lst) count)
            ((LISTP (car lst)) (setq count (+ count 1)) (detlen (cdr lst) count))
            (t (detlen (cdr lst) count))
    )
)
Run Code Online (Sandbox Code Playgroud)

lisp clisp list common-lisp depth

1
推荐指数
1
解决办法
55
查看次数

标签 统计

clisp ×1

common-lisp ×1

depth ×1

lisp ×1

list ×1