方案:列表的 CAR 和 CDR
我对 car
和 cdr
如何在列表上工作感到困惑。这是我尝试过的示例:
(define sample (read))
(display sample)
(display (car sample))
(display (cdr sample))
(display (car (cadr sample)))
(display (cdr (cdr sample)))
在输入值 '(ABCDEF)
时,我得到的结果如下:
'(a b c d e f)
quote
((a b c d e f))
a
()
我无法理解 quote
是如何实现的样本
的汽车
。另外,为什么(cdr example)
输出((abcdef))
?
语言:DrScheme - R5RS - 方案
I am confused as to how car
and cdr
work on lists. Here is an example of what I have tried:
(define sample (read))
(display sample)
(display (car sample))
(display (cdr sample))
(display (car (cadr sample)))
(display (cdr (cdr sample)))
On entering the value '(A B C D E F)
, here is what I get:
'(a b c d e f)
quote
((a b c d e f))
a
()
I am not able to understand that how quote
can be the car
of sample
. Also, why does (cdr sample)
output ((a b c d e f))
?
Language: DrScheme - R5RS - Scheme
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想简单地键入列表
(abcdef)
,则只需键入(abcdef)
。相反,您输入的是(quote (abcdef))
,因为'
运算符是(quote ...)
的缩写。您的列表实际上包含第一个元素
quote
和第二个元素(abcdef)
。当然,当您编写源代码时,您需要引号
来防止S表达式被执行。If you wanted to simply type the list
(a b c d e f)
, you should just type(a b c d e f)
. What you typed, instead, was(quote (a b c d e f))
because the'
operator is short for(quote ...)
.Your list literally has the first element
quote
and the second element(a b c d e f)
. Of course, when you're writing source code, you need thequote
to prevent the S-expressions from being executed.