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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With