函数名称上的未绑定变量

发布于 2024-10-05 19:46:53 字数 352 浏览 0 评论 0原文

我正在用 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)))
      )
)

该函数返回错误“未绑定变量:LLENGTH”,我不明白为什么或如何修复它。 有什么建议吗?

I'm writing a program in Lisp(common lisp dialect)..
I want the program to count the number of sublists in a list..
This is what I have written till now:

(defun llength (L)
      (cond 
          ((null L)   0)    
          ((list (first L)) (progn (+ (llength (first L)) 1) (llength (rest L))))    
          ((atom (first L)) (llength (rest L)))
      )
)

The function returns the error "Unbound variable: LLENGTH" and I don't understand why or how I can fix it..
Any suggestions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白云悠悠 2024-10-12 19:46:53

您的代码中有多个错误。

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

其次,(progn (+ (llength (first L)) 1) (llength (rest L)) 不会增加计数器。progn 逐个执行表达式并返回 < strong>最后一个表达式的结果,其他结果主要是为了副作用而被丢弃:1表示找到了一个。列表,将函数应用于第一个元素的结果和应用于其余元素的结果因此,这一行必须是:

((listp (first L)) (+ (llength (first L)) (llength (rest L)) 1))

可能存在更多错误,请小心正确缩进代码 - 这确实有助于减少错误。

You have multiple errors in your code.

First of all, list function creates new list, not checking if it is a list. The function you need is listp - "p" at the end means "predicate".

Second, (progn (+ (llength (first L)) 1) (llength (rest L)) will not increase counter. progn performs expressions one by one and returns result of the last expression, other results are just thrown out. progn is there mostly for side effects. What you actually need is addition of all three components: 1 to indicate one found list, result of applying function to the first element and result for applying to the rest. So, this line must be:

((listp (first L)) (+ (llength (first L)) (llength (rest L)) 1))

More errors may exist, please, be careful to indent code correctly - it really helps to reduce them.

丢了幸福的猪 2024-10-12 19:46:53

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

(function name (parameters))

也许您只是键入:

function name (parameters)

这样做会导致您收到错误,因此确保将您的整个陈述包含在括号中。

When you define a function with the (defun function name (parameters)) call you must then call the function by typing:

(function name (parameters))

Perhaps you were simply typing:

function name (parameters)

Doing this will get you the error you are receiving so be sure to encompass your whole statement in parenthesis.

好听的两个字的网名 2024-10-12 19:46:53
(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)))))

测试:

> (llength '(1 2 3 4))
0
> (llength '(1 2 (3 4)))
1
> (llength '(1 2 (3 (4))))
2
> (llength '(1 2 (3 4) (5 6) (7 8) (9 (10 (11)))))
6
(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)))))

Test:

> (llength '(1 2 3 4))
0
> (llength '(1 2 (3 4)))
1
> (llength '(1 2 (3 (4))))
2
> (llength '(1 2 (3 4) (5 6) (7 8) (9 (10 (11)))))
6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文