方案中的 car 和 cdr 让我发疯

发布于 2024-08-30 18:44:54 字数 503 浏览 4 评论 0原文

我遇到了 carcdr 函数的问题

例如,

:首先我定义了一个名为 x 的列表

(define x (a (bc) d ( (ef) g ) ))

,所以 x 现在等于 (a (bc ) d ( (ef) g ) )

例如,现在我需要仅使用 carcdrg > (!! noshortcuts as caddr cddr !!) 正确答案是:

(car(cdr(car(cdr(cdr(cdr x))))))

但是如何呢?我按照规则工作(car 给出列表的头部,cdr 给出列表的尾部),

但我没有得到上面的答案,而是不断得到错误的答案。 谁能帮助我理解这一点......给我一个步骤或一步一步解决它的方法?

I'm facing a problem with the car and cdr functions

for example:

first I defined a list called it x

(define x (a (bc) d ( (ef) g ) ))

so x now is equal to (a (bc) d ( (ef) g ) )

now for example I need to get the g from this list using only car and cdr
(!! noshortcuts as caddr cddr !!) the correct answer is:

(car(cdr(car(cdr(cdr(cdr x))))))

But how? I work according to the rules (the car gives the head of the list and cdr gives the tail)

and instead of getting the answer above, I keep reaching wrong answers.
Can anyone help me in understanding this ... give me a step or a way to solve it step by step?

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

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

发布评论

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

评论(6

╰つ倒转 2024-09-06 18:44:54

尝试一步步执行:

  • cdr 生成没有第一个元素的列表
  • car 生成列表的第一个元素

     x 是 (a (bc) d ( (ef) g ))
                             (cdr x) 是 ( (bc) d ( (ef) g ))
                        (cdr (cdr x)) 是 ( d ( (ef) g ))
                   (cdr (cdr (cdr x))) 是 ( ( (ef) g ))
              (car (cdr (cdr (cdr x)))) 是 ( (ef) g )
         (cdr (car (cdr (cdr (cdr x))))) 是 ( g )
    (汽车(cdr(汽车(cdr(cdr(cdr x))))))是 g
    

Try to do it step by step:

  • cdr yields the list without the first element
  • car yields the first element of a list

                                  x         is  (a (bc) d ( (ef) g ))
                             (cdr x)        is  (  (bc) d ( (ef) g ))
                        (cdr (cdr x))       is  (       d ( (ef) g ))
                   (cdr (cdr (cdr x)))      is  (         ( (ef) g ))
              (car (cdr (cdr (cdr x))))     is            ( (ef) g )
         (cdr (car (cdr (cdr (cdr x)))))    is            (      g )
    (car (cdr (car (cdr (cdr (cdr x))))))   is                   g
    
两个我 2024-09-06 18:44:54

一次进行一个转换。 cdr 为您提供一个没有第一个元素的列表,car 为您提供第一个元素。

(cdr (a (bc) d ( (ef) g ) )) -> ( (bc) d ( (ef) g ) )
(cdr ( (bc) d ( (ef) g ) ))  -> ( d ( (ef) g ) )
(cdr ( d ( (ef) g ) ))       -> ( ( (ef) g ) )
(car ( ( (ef) g ) ))         -> ( (ef) g )  <- pulls the first element out, which happens to be a list itself
(cdr ( (ef) g ))             -> (g)
(car (g))                    -> 'g

do the transforms one at a time. cdr gives you a list without the first element, car gives you the first element.

(cdr (a (bc) d ( (ef) g ) )) -> ( (bc) d ( (ef) g ) )
(cdr ( (bc) d ( (ef) g ) ))  -> ( d ( (ef) g ) )
(cdr ( d ( (ef) g ) ))       -> ( ( (ef) g ) )
(car ( ( (ef) g ) ))         -> ( (ef) g )  <- pulls the first element out, which happens to be a list itself
(cdr ( (ef) g ))             -> (g)
(car (g))                    -> 'g
肤浅与狂妄 2024-09-06 18:44:54

这是获取列表值的简单/紧凑的方法。

(cadr (cadddr x))

通过组合重复函数,您可以获得优雅且易于阅读的语句。

this is easy/compact way to get the value of the list.

(cadr (cadddr x))

by combining repeating functions, you get elegant easy to read statement.

辞别 2024-09-06 18:44:54
(cdr x) = ((bc) d ( (ef) g ) )
(cdr(cdr x)) = (d ( (ef) g ) )
(cdr(cdr(cdr x))) = (( (ef) g ) )
(car(cdr(cdr(cdr x)))) = ( (ef) g )
(cdr(car(cdr(cdr(cdr x))))) = (g)
(car(cdr(car(cdr(cdr(cdr x)))))) = g
(cdr x) = ((bc) d ( (ef) g ) )
(cdr(cdr x)) = (d ( (ef) g ) )
(cdr(cdr(cdr x))) = (( (ef) g ) )
(car(cdr(cdr(cdr x)))) = ( (ef) g )
(cdr(car(cdr(cdr(cdr x))))) = (g)
(car(cdr(car(cdr(cdr(cdr x)))))) = g
翻了热茶 2024-09-06 18:44:54

您是否尝试过使用 REPL(read-eval-print-loop),例如 CSIS?这样您就可以交互式地解决这个问题,这将使您更容易使用Scheme 学习和解决这个(以及其他)问题。

Have you tried using a REPL (read-eval-print-loop) such as csi? That way you can work on this interactively, which will make it easier for you to learn and work through this (and other) problems using Scheme.

清秋悲枫 2024-09-06 18:44:54

迭代地进行。另外,要意识到该计划总是向后看。

(cdr x) = ( (b c) ...)
(cdr (cdr (cdr x))) = (( (ef) g))
(car (cdr (cdr (cdr x)))) = ((ef) g)
(cdr (car (cdr (cdr (cdr x))))) = (g)
(car (cdr (car (cdr (cdr (cdr x)))))) = 'g

希望有帮助

do it iteratively. Also, realize that scheme always looks backwards.

(cdr x) = ( (b c) ...)
(cdr (cdr (cdr x))) = (( (ef) g))
(car (cdr (cdr (cdr x)))) = ((ef) g)
(cdr (car (cdr (cdr (cdr x))))) = (g)
(car (cdr (car (cdr (cdr (cdr x)))))) = 'g

hope that helps

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