Symbols ======= Datatype for 'names', where we care mainly about equality, not details of the name like its length, individual characters, etc. Historical, used for efficiency. Like enumerated constants in C/C++. In Java we might use distinct constant ints, using only == on them and never do arithmetic with them. 'abc => abc 'aBc => abc ; case insensitive, usually shown lower case Two main operations: (symbol? 'a) => #t (symbol? "a") => #f (symbol? a) ; error, a not bound (define a 'b) (eq? a 'a) => #f (eq? a 'b) => #t Equality ======== Briefly: = for numbers eq? for same objects eqv? for numbers and symbols that have same values equal? for strings, lists E.g, > (eq? '(a (b c)) '(a (b c))) #f > (eqv? '(a (b c)) '(a (b c))) #f > (equal? '(a (b c)) '(a (b c))) #t In fact equal? subsumes = and eq?, but may be slower due to type tests. Quoting ======= Quoting may also be used for literal lists. It prevents evaluation of elements normally treated as variables or procedure/syntactic-form execution. E.g. (define a 2) (+ a 1) => 3 '(+ a 1) => (+ a 1) '(1 a "a" (+ a 1)) => (1 a "a" (+ a 1)) (list 1 a "a" (+ a 1)) => (1 a "a" 3) ;explain what is (list ...) (car ''a) ; (car (quote (quote a))) => quote ; first element of list of symbols (quote a) Use it only if the *entire list* is constant: known before even running the program. Quote is not necessary for lists: the list procedure can always be used: (list '+ 'a '1) => (+ a 1) (list '+ 'a 1) => (+ a 1) ; quoting only significant for symbols and lists Quasiquote and unquote ========================= Quote is a convenience for entirely literal lists: it evaluates none of its arguments. The list procedure evaluates all of its arguments, and one can selectively quote to get literal values as some of the elements. Quasiquote doesn't evaluate its arguments, except one can selectively unquote to get back to evaluating some of the elements. So whether one uses quasiquote or the list procedure is a matter of style, which depends on the amount of literal data. (define a 1) (define b 2) (define c 3) (list a b (- c)) => (1 2 -3) '(a b (- c)) => (a b (- c)) (list 'a b '(- c)) => (a 2 (- c)) `( a b (- c)) => (a b (- c)) `(,a b ,(- c)) => (1 b -3) `(,a b (- ,c)) => (1 b (- 3)) If you see only one kind of quote here, change your font. `... is shorthand for (quasiquote ...) ,... is shorthand for (unquote ...) An example of recursion: ================================ If there is still time, show another recursion example by using helper functions. Write a function called swapTwoInLists that takes a list L whose elements are themselves lists with at least two elements, and returns a list of all the elements in all the lists in L, but with the first two elements in each list swapped. e.g. > (swapTwoInLists '((1 2 3) (4 5 6) (7 8)) ) (2 1 3 5 4 6 8 7) (define (swapTwo list) (cons (cadr list) (cons (car list) (cddr list)))) (define (swapTwoInLists list) (cond ((null? list) '()) (else (append (swapTwo (car list)) (swapTwoInLists (cdr list)))))) If there is still time, use let.