Lisp 确定列表是否包含列表
我想检查LISP中的列表是否递归地包含list。如何修复此代码Lisp 确定列表是否包含列表,lisp,Lisp,我想检查LISP中的列表是否递归地包含list。如何修复此代码 (defun has-list-p(l) (if null l) nil (and (listp(car l)) (has-list-p(l)))) 谢谢大家!我编写的解决方案是: (defun has-list-p(l) (if (null l) nil (or (listp(car l)) (has-list-p(cdr l))))) 我不会给你代
(defun has-list-p(l)
(if null l)
nil
(and (listp(car l)) (has-list-p(l))))
谢谢大家!我编写的解决方案是:
(defun has-list-p(l)
(if (null l)
nil
(or (listp(car l)) (has-list-p(cdr l)))))
我不会给你代码,虽然我已经写了。不过,我会告诉你你做错了什么:
- 检查行
。你少了一个帕伦(如果为空l)
- 你确定
和
是你想要的比较器吗
- 您递归调用的是
,这是正确的,但您希望在列表的其余部分调用它——除了第一个元素之外的所有元素。你怎么会这样has\u list\u p
祝你好运和快乐 我不会给你代码,尽管我已经写了。不过,我会告诉你你做错了什么:
(defun has-list-p(l)
;nothing more to check - return nil - no inner lists
(if (null l)
nil
;the first element of the list is a list?
(if (listp (car l))
;if yes - return true
t
;otherwise - try for the cdr of the list
(has-list-p (cdr l)))))
- 检查行
。你少了一个帕伦(如果为空l)
- 你确定
和
是你想要的比较器吗
- 您递归调用的是
,这是正确的,但您希望在列表的其余部分调用它——除了第一个元素之外的所有元素。你怎么会这样has\u list\u p
(defun has-list-p(l)
;nothing more to check - return nil - no inner lists
(if (null l)
nil
;the first element of the list is a list?
(if (listp (car l))
;if yes - return true
t
;otherwise - try for the cdr of the list
(has-list-p (cdr l)))))
在common lisp中,在我定义此过程后,它将打印:
[2]> (has-list-p '(1 2))
NIL
[3]> (has-list-p '(1 2 '(3 4)))
T
在Emacs Lisp中也是如此
在common lisp中,在我定义此过程后,它将打印:
[2]> (has-list-p '(1 2))
NIL
[3]> (has-list-p '(1 2 '(3 4)))
T
在Emacs Lisp中也是如此。lol,我正要用代码发布一个答案,但我更喜欢你的方法:)lol,我正要用代码发布一个答案,但我更喜欢你的方法:)除了空格之外,这和我写的代码完全一样。你亲自解决了这个问题,我向你致敬。干得好。撇开空格不谈,这和我写的代码完全一样。你亲自解决了这个问题,我向你致敬。做得好。