[英]Scheme: CAR and CDR of a list
I am confused as to how car
and cdr
work on lists. 我很困惑,如何
car
和cdr
上列出的工作。 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 '(ABCDEF)
, here is what I get: 在输入值
'(ABCDEF)
,我得到的是:
'(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
. 我无法理解
quote
如何成为sample
car
。 Also, why does (cdr sample)
output ((abcdef))
? 另外,为什么
(cdr sample)
输出((abcdef))
?
Language: DrScheme - R5RS - Scheme 语言:DrScheme - R5RS - Scheme
If you wanted to simply type the list (abcdef)
, you should just type (abcdef)
. 如果你只想输入列表
(abcdef)
,你应该输入(abcdef)
。 What you typed, instead, was (quote (abcdef))
because the '
operator is short for (quote ...)
. 而你输入的是
(quote (abcdef))
因为'
运算符是(quote ...)
缩写。
Your list literally has the first element quote
and the second element (abcdef)
. 你的列表字面上有第一个元素
quote
和第二个元素(abcdef)
。 Of course, when you're writing source code, you need the quote
to prevent the S-expressions from being executed. 当然,在编写源代码时,需要使用
quote
来防止执行S表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.