CLISP 的嵌套列表和 lambda 表达式存在一些问题

发布于 2024-12-02 19:11:46 字数 567 浏览 2 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

粉红×色少女 2024-12-09 19:11:46

首先,除非你错误地输入了 if 而不是 iff,否则问题是相当微不足道的,只需返回 t 就可以了:-)

说实话相反,当您需要使用递归解决问题时,想法通常很简单:

  1. 如果我们处于一个微不足道的情况下,只需返回答案
  2. ,否则答案与我们通过解决问题得到的答案相同,只是稍微简单一点这个,我们称之为我们自己来解决这个简化版本。

具体考虑:

  1. 如果第二个参数是空列表,则答案为 NIL
  2. 如果第一个参数等于第二个参数的第一个元素,则只需返回 T
  3. 否则,如果第二个列表的第一个元素是一个列表(因此也可能是一个嵌套列表)并且该元素包含在这个多重列表中,则返回 true (为了检查这种情况,函数正在调用自身)
  4. 否则只需检查相同的问题,但是首先删除第二个参数的第一个元素,因为它已经被检查过(这也递归地调用相同的函数)

所以基本上 1 和 2 是简单的情况; 3 和 4 是您解决问题的更简单版本的情况。

First of all unless you mistyped if instead of iff the problem is quite trivial, just return t and you're done :-)

Seriously speaking instead when you need to solve a problem using recursion the idea often is simply:

  1. if we're in a trivial case just return the answer
  2. otherwise the answer is the same answer we'd get by solving a problem that it's just a little bit simpler that this, and we call ourself to solve this simplified version.

In the specific consider:

  1. If the second argument is an empty list the answer is NIL
  2. If the first argument is equal to the first element of the second argument then just return T instead
  3. Otherwise if the first element of the second list is a list (therefore also possibly a nested list) and the element is contained in this multilist then return true (to check this case the function is calling itself)
  4. Otherwise just check the same problem, but first dropping the first element of the second argument, because it has been checked (this also calls recursively the same function)

So basically 1 and 2 are the trivial cases; 3 and 4 are the cases in which you solve a simpler version of the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文