How do lists work in Racket?

Short answer

Build lists with list or cons, take them apart with first and rest, and test for the end with empty?. cons adds to the front and returns a new list.

racket
(define tracks (list "python" "php" "ruby"))
(define nums '(4 8 15 16 23 42))      ; quote is shorthand

(first tracks)          ; "python"
(rest tracks)           ; '("php" "ruby")
(length nums)           ; 6
(list-ref nums 2)       ; 15
(reverse nums)
(append '(1 2) '(3 4))  ; '(1 2 3 4)
(cons "scratch" tracks) ; a NEW list with "scratch" on the front

#Why the quote

racket
'(4 8 15)     ; a list of three numbers
(4 8 15)      ; ERROR — tries to call 4 as a function

Code and data have the same shape in Lisp-family languages, so the quote is how you say "this is data, do not evaluate it".

#cons builds, it does not modify

racket
(define a '(2 3))
(define b (cons 1 a))
a      ; still '(2 3)
b      ; '(1 2 3)

Lists are immutable. cons is cheap because b shares the whole of a rather than copying it.

#Adding to the front is fast; the end is not

(cons x lst) is constant time. (append lst (list x)) copies the entire list. When building a list in a loop, cons onto the front and reverse once at the end.

#Higher-order functions

racket
(map    (lambda (n) (* n 2)) '(1 2 3))    ; '(2 4 6)
(filter even? '(1 2 3 4))                 ; '(2 4)
(foldr  + 0 '(1 2 3 4))                   ; 10
(apply  max '(4 8 15))                    ; 15

These came from Lisp in 1958 and are why Python, Ruby and JavaScript all have map and filter today.