Variables 函数名上的未绑定变量

Variables 函数名上的未绑定变量,variables,lisp,common-lisp,Variables,Lisp,Common Lisp,我正在用Lisp(通用Lisp方言)编写一个程序。。 我想让程序计算列表中的子列表数。。 这是我到现在为止写的: (defun llength (L) (cond ((null L) 0) ((list (first L)) (progn (+ (llength (first L)) 1) (llength (rest L)))) ((atom (first L)) (llength (rest L))

我正在用Lisp(通用Lisp方言)编写一个程序。。 我想让程序计算列表中的子列表数。。 这是我到现在为止写的:

(defun llength (L)
      (cond 
          ((null L)   0)    
          ((list (first L)) (progn (+ (llength (first L)) 1) (llength (rest L))))    
          ((atom (first L)) (llength (rest L)))
      )
)
函数返回错误“Unbound variable:LLENGTH”,我不明白为什么或者如何修复它。。
有什么建议吗

您的代码中有多个错误

首先,
list
函数创建新列表,不检查它是否为列表。您需要的函数是
listp
-“p”结尾表示“谓词”

第二,
(程序(+(长度(第一个L))1)(长度(剩余L))
不会增加计数器。
progn
逐个执行表达式并返回最后一个表达式的结果,其他结果将被抛出。
progn
主要用于副作用。您实际需要的是添加所有三个组件:1表示一个找到的列表,应用函数的结果返回到第一个元素,并将结果应用到其余元素。因此,此行必须为:

((listp (first L)) (+ (llength (first L)) (llength (rest L)) 1))
可能存在更多错误,请注意正确缩进代码-这确实有助于减少错误

(defun llength (list)
  (cond 
    ((null list) 0)
    ((listp (first list))
     ;; 1 + the count of any sub-lists in this sub-list + the 
     ;; count of any sub-lists in the rest of the list.
     (+ 1 (llength (first list))
        (llength (rest list))))
    (t (llength (rest list)))))
测试:


使用
(defun function name(parameters))
调用定义函数时,必须通过键入以下内容来调用该函数:

(function name (parameters))
也许你只是在打字:

function name (parameters)

这样做会得到您收到的错误,因此请确保将整个语句包含在括号中。

是的,请重新格式化代码,使其可读
function name (parameters)