This is a small sheet that reviews list operations. We'll be covering the following: car, cdr, list, cons, append For all these examples, assume that 'L' stands for a list of some kind. 'E' is an element --- it can be anything (list, number, symbol). --- car --- usage: (car L) description: This returns the first element of a list. examples: (car '(this is a test)) => 'this (car '(1)) => 1 (car '()) => error --- we can't take the car of an empty list. (car '((this is) nested)) => '(this is) --- lists are also elements. --- cdr --- usage: (cdr L) description: This returns the rest of the list. What we mean by the "rest" is the list with the first element taken out. examples: (cdr '(this is a test)) => '(is a test) --- cross out 'this. (cdr '(1)) => '() --- cross out 1. (cdr '()) => error --- we can't take the cdr of an empty list either. (cdr '((this is) nested)) => '(nested) --- cross out the first element '(this is) --- list --- usage: (list E1 E2 ... ) --- element can be anything. Can take any number of arguments. description: Creates a list with elements: E1, E2, ..., and so on. hint: Here's a procedure for getting list right. Suppose that we're trying out: (list 1 2 3 '4 '(five)) 1. Write down all the arguments in a line, taking out all quotes 1 2 3 4 (five) 2. Put parentheses around the whole thing, and begin it with a quote. '(1 2 3 4 (five)) 3. That's it. This is a list of 5 elements. examples: (list 'this 'is 'a 'test) => '(this is a test) (list '(1)) => '((1)) --- note: you CAN make a list of one element. (list '()) => '(()) (list (list 'this 'is) 'nested) => (list '(this is) 'nested) => '((this is) nested) --- cons --- usage: (cons E L) description: Attach element E to the front of the list L and return that list. hint: Let's try: (cons 1 '(2)). 1. Copy down the arguments to cons, taking out the quotes. 1 (2) 2. Next, move the left parens of the second argument to enclose the first. 1 (2) ^------ move this parenthesis left (1 2) 3. Add a quote in the front, and we're done. '(1 2) examples: (cons 'hello '(world)) => '(hello world) (cons '(alpha) '(beta)) => '((alpha) beta) (cons 'slashdot '((kuro5hin kenshin) (cowboy bebop))) => '(slashdot (kuro5hin kenshin) (cowboy bebop)) --- append --- usage: (append L1 L2 ... LN) description: Take the elements of L1, L2, ... LN, and put them all into a single list. hint: Let's try (append '(1 one) '(2 (two)) '((3) three)): 1. Write out L1, L2, ... LN, each without the outermost parentheses: 1 one 2 (two) (3) three 2. Put paretheses around the whole thing, and top it with a quote. '(1 one 2 (two) (3) three) examples: (append '(zerg) '(vs) '(protoss)) => '(zerg vs protoss) (append '(Harkonnen) '((vs)) '(Atreides)) => '(Harkonnen (vs) Atreides) (append '() '()) => '() --- yes, this is silly, but it works. (append '(unix) (list 'dos 'threads)) => (append '(unix) '(dos threads)) => '(unix dos threads)