Scheme: CAR and CDR of a list
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
()
开发者_Go百科
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
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 the quote
to prevent the S-expressions from being executed.
精彩评论