Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
()

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

like image 868
name_masked Avatar asked Jul 03 '10 19:07

name_masked


1 Answers

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.

like image 156
erjiang Avatar answered Oct 03 '22 21:10

erjiang