> (car foo) ; car gets the first element in a pair. 1 > (cdr foo) ; while cdr gets the second 2
Lists
Pair is basically a little list.
The definition of a list is a set of pairs and the last pair in that has null on the right hand side. If you don’t believe that, check out the code below.
1 2 3 4 5 6 7 8 9 10 11 12
> (cons1 null) ; it's indeed a pair of 1 and null but the way the interpreter writes it is a list containing only 1. (1)
> (cons12) (1 . 2) ; it's a pair. > (cons1 (cons2 null)) (12) ; it's a list > (cons1 (cons2 (cons3 null))) (123)
> (equal? (cons1 (cons2 null)) (list12)) #t
List is a bunch of pairs cascading down and ending in null.
> (car mylist) 1; the first element 1 in the pair mylist > (cdr mylist) (23) ; the second element (cons 2 (cons 3 null)) in the pair mylist, which is precisely the rest of the list mylist.
> (cadr mylist) ; d, then a. 2 > (caddr mylist) ; double d, then a. 3
Map applies the function you give it to every element in the list you give it. Difference between it and Python’s map is the latter one returns a map object which is a kind of iterator, but Scheme’s map directly give you a list.