CLISP 的嵌套列表和 lambda 表达式存在一些问题
Common Lisp 中的嵌套列表确实让我很困惑。问题如下:
通过使用递归,让 (nested-list 'b '(a (bc) d))
返回 t
如果第一个参数出现在第二个参数中(可能是 嵌套列表),否则nil
。
我尝试了find
,但仅当第一个参数为'(bc)
时才有效>。 我把目光转向了 lambda 表达式。我想压平第二个 首先使用参数,然后使用 eq 来比较参数。
(defun nested-list (x y)
(cond
((null y) ())
(t (append (lambda (flatten) (first y))
然后我就卡住了。尽管我读了很多关于 lambda 的东西 表达,它仍然让我困惑。不知道什么时候才能回忆起来 我需要,我知道 funcall 函数,但你知道我就是无法得到 它。我刚刚学习了 Common Lisp 5 天,所以我希望你能给我一个 暗示。多谢!
Nested lists in Common Lisp really confused me. Here is the problem:
By using recursion, let (nested-list 'b '(a (b c) d))
return t
if the first argument appears in the second argument (which could be
a nested list), and nil
otherwise.
I tried find
, but it only works if the first argument is '(b c)
.
I turned my eyes on lambda expressions. I want to flatten the second
argument first, and then use eq
to compare the arguments.
(defun nested-list (x y)
(cond
((null y) ())
(t (append (lambda (flatten) (first y))
Then I got stuck. Even though I read a lot of stuff about lambda
expessions, it still confused me. I do not know how to recall it when
I need, I knew the funcall function, but you know I just cannot get
it. I just learnt Common Lisp for 5 days, so I hope you can give me a
hint. Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,除非你错误地输入了
if
而不是iff
,否则问题是相当微不足道的,只需返回t
就可以了:-)说实话相反,当您需要使用递归解决问题时,想法通常很简单:
具体考虑:
NIL
T
所以基本上 1 和 2 是简单的情况; 3 和 4 是您解决问题的更简单版本的情况。
First of all unless you mistyped
if
instead ofiff
the problem is quite trivial, just returnt
and you're done :-)Seriously speaking instead when you need to solve a problem using recursion the idea often is simply:
In the specific consider:
NIL
T
insteadSo basically 1 and 2 are the trivial cases; 3 and 4 are the cases in which you solve a simpler version of the problem.