简体   繁体   中英

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 '(ABCDEF) , 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 ((abcdef)) ?

Language: DrScheme - R5RS - Scheme

If you wanted to simply type the list (abcdef) , you should just type (abcdef) . What you typed, instead, was (quote (abcdef)) because the ' operator is short for (quote ...) .

Your list literally has the first element quote and the second element (abcdef) . Of course, when you're writing source code, you need the quote to prevent the S-expressions from being executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM