#### 3.8Pairs and Lists

A pair joins two arbitrary values. The cons procedure constructs pairs, and the car and cdr procedures extract the first and second elements of the pair, respectively. The pair? predicate recognizes pairs.

Some pairs print by wrapping parentheses around the printed forms of the two pair elements, putting a ' at the beginning and a . between the elements.

Examples:
 > (cons 1 2) '(1 . 2) > (cons (cons 1 2) 3) '((1 . 2) . 3) > (car (cons 1 2)) 1 > (cdr (cons 1 2)) 2 > (pair? (cons 1 2)) #t

A list is a combination of pairs that creates a linked list. More precisely, a list is either the empty list null, or it is a pair whose first element is a list element and whose second element is a list. The list? predicate recognizes lists. The null? predicate recognizes the empty list.

A list normally prints as a ' followed by a pair of parentheses wrapped around the list elements.

Examples:
 > null '() > (cons 0 (cons 1 (cons 2 null))) '(0 1 2) > (list? null) #t > (list? (cons 1 (cons 2 null))) #t > (list? (cons 1 2)) #f

A list or pair prints using list or cons when one of its elements cannot be written as a quoted value. For example, a value constructed with srcloc cannot be written using quote, and it prints using srcloc:

 > (srcloc "file.rkt" 1 0 1 (+ 4 4)) (srcloc "file.rkt" 1 0 1 8) > (list 'here (srcloc "file.rkt" 1 0 1 8) 'there) (list 'here (srcloc "file.rkt" 1 0 1 8) 'there) > (cons 1 (srcloc "file.rkt" 1 0 1 8)) (cons 1 (srcloc "file.rkt" 1 0 1 8)) > (cons 1 (cons 2 (srcloc "file.rkt" 1 0 1 8))) (list* 1 2 (srcloc "file.rkt" 1 0 1 8))

As shown in the last example, list* is used to abbreviate a series of conses that cannot be abbreviated using list.

The write and display functions print a pair or list without a leading ', cons, list, or list*. There is no difference between write and display for a pair or list, except as they apply to elements of the list:

Examples:
 > (write (cons 1 2)) (1 . 2) > (display (cons 1 2)) (1 . 2) > (write null) () > (display null) () > (write (list 1 2 "3")) (1 2 "3") > (display (list 1 2 "3")) (1 2 3)

Among the most important predefined procedures on lists are those that iterate through the list’s elements:

 > (map (lambda (i) (/ 1 i)) '(1 2 3))

'(1 1/2 1/3)

 > (andmap (lambda (i) (i . < . 3)) '(1 2 3))

#f

 > (ormap (lambda (i) (i . < . 3)) '(1 2 3))

#t

 > (filter (lambda (i) (i . < . 3)) '(1 2 3))

'(1 2)

 > (foldl (lambda (v i) (+ v i)) 10 '(1 2 3))

16

 > (for-each (lambda (i) (display i)) '(1 2 3))

123

 > (member "Keys" '("Florida" "Keys" "U.S.A."))

'("Keys" "U.S.A.")

 > (assoc 'where '((when "3:30") (where "Florida") (who "Mickey")))

'(where "Florida") Pairs and Lists in The Racket Reference provides more on pairs and lists.

Pairs are immutable (contrary to Lisp tradition), and pair? and list? recognize immutable pairs and lists, only. The mcons procedure creates a mutable pair, which works with set-mcar! and set-mcdr!, as well as mcar and mcdr. A mutable pair prints using mcons, while write and display print mutable pairs with { and }:

Examples:
 > (define p (mcons 1 2)) > p (mcons 1 2) > (pair? p) #f > (mpair? p) #t > (set-mcar! p 0) > p (mcons 0 2) > (write p) {0 . 2} Mutable Pairs and Lists in The Racket Reference provides more on mutable pairs.
{"ft_lang_label":"__label__en","ft_lang_prob":0.69548273,"math_prob":0.8519388,"size":3220,"snap":"2020-24-2020-29","text_gpt3_token_len":1093,"char_repetition_ratio":0.14396766,"word_repetition_ratio":0.10989011,"special_character_ratio":0.3720497,"punctuation_ratio":0.11571429,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97527283,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-08T01:10:58Z\",\"WARC-Record-ID\":\"<urn:uuid:18063fc1-b00a-4d8c-9058-8bf4a71b7fd3>\",\"Content-Length\":\"60916\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5c59067e-271d-4e0e-a5c6-c342d9609d37>\",\"WARC-Concurrent-To\":\"<urn:uuid:d1f23d34-ca90-4325-a04a-422f40c6d482>\",\"WARC-IP-Address\":\"104.24.100.149\",\"WARC-Target-URI\":\"https://download.racket-lang.org/releases/7.7/doc/guide/pairs.html\",\"WARC-Payload-Digest\":\"sha1:MYFR5QLYH2KWW6NREZHCK6TM6TUE3OQU\",\"WARC-Block-Digest\":\"sha1:CWACWMTC2EKD2T5BPQNCK5RVMUY3TD3S\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655896169.35_warc_CC-MAIN-20200708000016-20200708030016-00466.warc.gz\"}"}