区分 Common Lisp 中的列表和原子
我正在制作一个基本的 clisp 函数,它只返回列表中的原子数。我遇到的问题是我需要它为列表中的原子递增,而不是将列表视为列表中的 1 个元素。
我猜真正的问题是如何在代码中区分元素是列表还是原子?如果我能做到这一点,我可以将列表发送到另一个函数来累加并返回它们包含的原子数。
清澈如泥? :)
我这里有一个例子:
(defun list_length (a)
(cond ((null a) 0)
(t (+ 1 (list_length (cdr a))))))
如果父列表中没有嵌入列表,这非常有用,例如, '(1 2 3 (4 5) 6)
将返回 5。我需要它包含 4 和 5,而不是列表 (4 5) 作为一个。
感谢您的帮助。
乔恩
编辑:
(defun list_length (a)
(cond ((null a) 0)
((listp (car a)) (list_length (car a)))
(t (+ 1 (list_length (cdr a))))))
[18]> (list_length '(1 2 3 (4 5) 6))
1. Trace: (LIST_LENGTH '(1 2 3 (4 5) 6))
2. Trace: (LIST_LENGTH '(2 3 (4 5) 6))
3. Trace: (LIST_LENGTH '(3 (4 5) 6))
4. Trace: (LIST_LENGTH '((4 5) 6))
5. Trace: (LIST_LENGTH '(4 5))
6. Trace: (LIST_LENGTH '(5))
7. Trace: (LIST_LENGTH 'NIL)
7. Trace: LIST_LENGTH ==> 0
6. Trace: LIST_LENGTH ==> 1
5. Trace: LIST_LENGTH ==> 2
4. Trace: LIST_LENGTH ==> 2
3. Trace: LIST_LENGTH ==> 3
2. Trace: LIST_LENGTH ==> 4
1. Trace: LIST_LENGTH ==> 5
5
[19]> (dribble)
I have a basic clisp function that I am making that just returns the number of atoms in a list. The issue I am having is I need it to increment for atoms in a list that is in the list, instead of seeing a list as 1 element in the list.
The real question I guess is how do you differentiate in your code whether an element is a list or an atom? If I can do that, I can send the lists to another function to add up and return the number of atoms they contain.
Clear as mud? :)
I have an example here:
(defun list_length (a)
(cond ((null a) 0)
(t (+ 1 (list_length (cdr a))))))
This works great if there are no embedded lists in the parent list, for example,'(1 2 3 (4 5) 6)
would return 5. I need it to include 4 and 5 instead of the list (4 5) as one.
Thanks for your help.
Jon
EDIT:
(defun list_length (a)
(cond ((null a) 0)
((listp (car a)) (list_length (car a)))
(t (+ 1 (list_length (cdr a))))))
[18]> (list_length '(1 2 3 (4 5) 6))
1. Trace: (LIST_LENGTH '(1 2 3 (4 5) 6))
2. Trace: (LIST_LENGTH '(2 3 (4 5) 6))
3. Trace: (LIST_LENGTH '(3 (4 5) 6))
4. Trace: (LIST_LENGTH '((4 5) 6))
5. Trace: (LIST_LENGTH '(4 5))
6. Trace: (LIST_LENGTH '(5))
7. Trace: (LIST_LENGTH 'NIL)
7. Trace: LIST_LENGTH ==> 0
6. Trace: LIST_LENGTH ==> 1
5. Trace: LIST_LENGTH ==> 2
4. Trace: LIST_LENGTH ==> 2
3. Trace: LIST_LENGTH ==> 3
2. Trace: LIST_LENGTH ==> 4
1. Trace: LIST_LENGTH ==> 5
5
[19]> (dribble)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
foo
是一个列表,(listp foo)
将返回t
,否则返回nil
。因此,您可以通过将以下情况添加到
cond
来使您的list_length
函数处理嵌套列表:(listp foo)
will returnt
iffoo
is a list andnil
otherwise.So you can make your
list_length
function handle nested lists by adding the following case to yourcond
:ATOM 是您要求的谓词。
我建议使用 FLATTEN,这是一种用于展平列表中列表的标准例程 - 我在这里介绍一个实现。
Flatten 返回一个列表:您可以在列表上运行 LENGTH 来查看最终有多少个 ATOMS。
ATOM is the predicate you are asking for.
I recommend using FLATTEN, a standard routine to flatten lists in lists - I present one implementation here.
Flatten returns a list: you can run LENGTH on the list to see how many ATOMS you wound up with.