开发者

Scheme: CAR and CDR of a list

开发者 https://www.devze.com 2023-01-06 02:51 出处:网络
I am confused as to how car and cdr work on lists.Here is an example of what I have tried: (define sample (read))

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消