2-1: Each of the following things may be an atom, a list, or neither. Identify each accordingly.
ATOM
(THIS IS AN ATOM)
(THIS IS AN EXPRESSION)
((A B) (C D) 3 (3)
(LIST 3)
(/ (+ 3 1) (- 3 1))
)(
((()))
(() ())
((())
())(
((ABC
2-2: Evaluate the follwoing forms:
(first '(p h w))
(rest '(b k p h))
(first '((a b) (c d)))
(rest '((a b) (c d)))
(first (rest '((a b) (c d))))
(rest (first '((a b) (c d))))
(rest (first (rest '((a b) (c d)))))
(first (rest (first '((a b) (c d)))))
2-3: Evaluate the follwoing forms:
(first (rest (first (rest '((a b) (c d) (e f))))))
(first (first (rest (rest '((a b) (c d) (e f))))))
(first (first (rest '(rest ((a b) (c d) (e f))))))
(first (first '(rest (rest ((a b) (c d) (e f))))))
(first '(first (rest (rest ((a b) (c d) (e f))))))
'(first (first (rest (rest ((a b) (c d) (e f))))))
2-4: Write sequences of FIRSTs and RESTs that will pick the symbol PEAR out of the follwoing expressions:
(apple orange pear grapefruit)
((apple orange) (pear grapefruit))
(((apple) (orange) (pear) (grapefruit)))
(apple (orange) ((pear)) (((grapefruit))))
((((apple))) ((orange)) (pear) grapefruit)
((((apple) orange) pear) grapefruit)
2-5: Evaluate the following slightly tricky forms:
(append '(a b c) '( ))
(list '(a b c) '( ))
(cons '(a b c) '( ))
2-6: Evaluate the following forms in the order given:
(setf tools (list 'hammer 'screwdriver))
(cons 'pliers tools)
tools
(setf tools (cons 'pliers tools)
tools
(append '(saw wrench) tools)
tools
(setf tools (append '(saw wrench) tools))
tools
2-7: Evaluate the following forms:
(cons (first nil) (rest nil))
2-8: Evaluate the following forms:
(length '(plato socrates aristotle))
(length '((plato) (socrates) (aristotle)))
(length '((plato socrates aristotle)))
(reverse '(plato socrates aristotle))
(reverse '((plato) (socrates) (aristotle)))
(reverse '((plato socrates aristotle)))
2-9: Evaluate the following forms:
(length '((car chevrolet) (drink coke) (cereal wheaties)))
(reverse '((car chevrolet) (drink coke) (cereal wheaties)))
(append '((car chevrolet) (drink coke)) (reverse '((car chevrolet) (drink coke))))
2-10: Evaluate the following forms:
(/ (+ 3 1) (- 3 1))
(* (MAX 3 4 5) (MIN 3 4 5))
(MIN (MAX 3 1 4) (MAX 2 7 1))
BASIC LISP PRIMITIVES ----------------------------------------------------------- LISP MEANS SYMBOL MANIPULATION * (+ 3.14 2.71) 5.85 * (setf friends '(dick jane sally)) * friends (DICK JANE SALLY) * (setf enemies '(troll grinch ghost)) * (setf enemies (remove 'ghost enemies)) * (setf friends (cons 'ghost friends)) * enemies (TROLL GRINCH) * friends (GHOST DICK JANE SALLY) (defun newfriend (name) (setf enemies (remove name enemies)) (Setf friends (cons name friends))) * (newfriend 'ghost) ------------------------------------------------------------- LISP PROCEDURES AND DATA ARE SYMBOLIC EXPRESSION * (* 9 3) 27 * (/ 27 3) 9 * (+ (* 2 2) (/ 2 2) 5 --------------------------------------------------------- FIRST AND REST TAKE LISTS APART * (first '(fast computers are nice)) FAST * (first '(a b c)) A * (rest '(fast computers are nice)) (COMPUTER ARE NICE) * (rest '(a b c)) (B C) * (rest '(C)) NIL * (first ()) NIL * (rest ()) NIL * (first '((a b) (c d))) (A B) ------------------------------------------------------ QUOTING STOPS EVALUATION * (first (rest (a b c))) !!! a is not a procedure * (first (rest '(a b c))) B * (first '(rest (a b c))) REST -------------------------------------------------- SOME OLD TIMERS USE CARS AND CDRS * (cadr '(a b c)) equivalent * (car (cde '(a b c))) equivalent * (first (rest '(a b c))) ------------------------------------------------------------ SETF ASSIGNS VALUES TO SYMBOLS * (setf ab-list '(a b)) * ab-list (A B) * 'ab-list ab-list * (first ab-list) A * (first 'ab-list) ERROR * (rest ab-list) (B) * (rest 'ab-list) ERROR ------------------------------------------------------ SETF ACCEPTS MULTIPLE SYMBOL-VALUE PAIRS * (setf ab-list '(a b) xy-list '(x y)) (X Y) * ab-list (A B) * xy-list (X Y) ------------------------------------------------------ CERTAINS ATOMS EVALUATE TO THEMSELVES * t T * nil NIL * (setf t nil) ERROR * 2 2 * 2.71 2.71 --------------------------------------------------- CONS, APPEND, AND LIST CONSTRUCT LISTS * (cons 'a '(b c)) (a b c) * (setf new-front 'a old-list '(b c)) * (cons new-front old-list) (A B C) * (first (cons new-font old-list)) A * (rest (cons new-front old-list)) (B C) (cons <a new element> <a list>) sample-list equivalent (cons (first sample-list) (rest sample-list)) * (append '(a b c) '(x y z)) (A B C X Y Z) * (setf ab-list '(a b) (xy-list '(x y)) * (append ab-list xy-list) (A B X Y) * (append ab-list xy-list ab-list) (A B X Y A B) * (append ab-list '() xy-list '()) (A B X Y) * (append 'ab-list xy-list) ERROR ;'AB-LIST is an atom, not a list. * (append '((a) (b)) '((c) (d))) ((A) (B) (C) (D)) * (list 'a 'b 'c) (A B C) * (setf front 'a middle 'b back 'c) * (list front middle back) (A B C) * (front middle back) ERROR * (setf ab-list '(a b)) * (list ab-list ab-list) ((A B) (A B)) * (list ab-list ab-list ab-list) ((A B) (A B) (A B)) * (list 'ab-list ab-list) (AB-LIST (A B)) * (setf ab-list '(a b) cd-list '(c d)) * (append ab-list cd-list) (A B C D) * (list ab-list cd-list) ((A B) (C D)) * (cons ab-list cd-list) ((A B) C D) * (append ab-list ab-list) (A B A B) * (list ab-list ab-list) ((A B) (A B)) * (cons ab-list ab-list) ((A B) A B) * (append 'ab-list ab-list) ERROR * (list 'ab-list ab-list) (AB-LIST (A B)) * (cons 'ab-list ab-list) (AB-LIST A B) --------------------------------------------------------- CONS, APPEND, AND LIST DO NOT ALTER SYMBOL VALUES * (setf new-front 'a old-list '(b c)) * (cons new-front old-list) (A B C) * new-front A * old-list (B C) (setf <name of a list> (cons <new element> <name of the list>)) * (setf new-front 'a list-to-be-changed '(b c)) * (setf list-to-be-changed (cons new-font list-to-be-changed)) (A B C) * new-front A * list-to-be-changed (A B C) * (setf new-front 'a list-to-be-changed '(b c)) * (push new-front list-to-be-changed) (A B C) * list-to-be-changed (A B C) * list-to-be-changed (A B C) * (pop list-to-be-changed) A * list-to-be-changed (B C) --------------------------------------------------------------- NTHCDR, BUTLAST, AND LAST SHORTEN LISTS * (setf abc-list '(a b c)) * (rest abc-list) (B C) * (nthcdr 2 abc-list) (C) * (setf abc-list '(a b c)) * (nthcdr 50 abc-list) NIL * (setf abc-list '(a b c)) * (butlast abc-list 2) (A) * (butlast abc-list 50) NIL * (setf abc-list '(a b c)) * (butlast abc-list) (A B) (setf f 'front b 'back abc-list '(a b c)) * (cons f abc-list) (FRONT A B C) * (append abc-list (list b)) (A B C BACK) * (setf abc-list '(a b c) ab-cd-list '((a b) (c d))) * (last abc-list) (C) * (last ab-cd-list) ((C D)) * (last 'abc-list) ERROR * (setf abc-list '(a b c)) * (last abc-list) (C) * (first (last abc-list)) C ------------------------------------------------------------------------------------- LENGTH and REVERSE Work on Top-Level Elements * (setf ab-list '(a b) ab-cd-list '(a b) (c d))) * (length ab-list) 2 * (length ab-cd-list) 2 * (length (append ab-list ab-list) 4 * (reverse ab-list) (B A) * (reverse ab-list) (B A) * (reverse ab-cd-list) ((C D) (A B)) * (reverse (append ab-list ab-list)) (B A B A) ----------------------------------------------------------------------------------- ASSOC Looks for Indexed Sublists * (setf sarah '((height .54) (weight 4.4))) (ASSOC <key> <association list>) * ('weight sarah) (WEIGHT 4.4) ---------------------------------------------------------------------------------- LISP Offers Integers, Ratios, and Floating-Point Numbers, among Others * (/ 1.234321 1.111) 1.111 * (/ 27 9) 3 * (/ 22 7) 22/7 * (float (/ 22 7)) 3.14286 * (round (/ 22 7)) ;Multiple-valued forms. 3; ;The nearest integer. 1/7 ;The remainder. * (+ (round (/ 22 7) (round (/ 7 3))) 5 * (round 5 2) 2; 1 * (+ 2 1.5) 3.5 * (+ (float 2) (float 2.5) 3.5 * (- 8) -8 * (- -8) 8 * (/ 1 2) 1/2 * (/ 2) 1/2 ----------------------------------------------------------------------------------------- A Few Primitives for Numbers Round Out a Basic Repertoire * (Max 2 4 3) 4 * (Min 2 4 3) 2 * (expt 2 3) 8 * (expt 3 2) 9 * (expt 3.3 2.2) 13.827085 * (expt 2.2 3.3) 13.48947 * (sqrt 9) 3 * (expt 3 2) 9 * (abs 5) 5 * (abs -5) 5 解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解解 2-1: atom; list; list; neither; list; list; neither; list; list; neither; neither; neither 2-2: P; (P K H); (A B); ((C D)); (C D); (B); (D); B 2-3: D; E; (A B); ERROR; FIRST; (FIRST (FIRST (REST (REST ((A B) (C D) (E F)))))) rest 一定回傳表 list,first 回傳原子 atom 2-4: (first (rest (rest '(apple orange pear grapefruit)))) (first (first (rest '((apple orange) (pear grapefruit))))) (first (first (rest (rest (first '(((apple) (orange) (pear) (grapefruit)))))))) (first (first (first (rest (rest '(apple (orange) ((pear)) (((grapefruit))))))))) (first (first (rest (rest '((((apple))) ((orange)) (pear) grapefruit))))) (first (rest (first '((((apple) orange) pear) grapefruit)))) 2-5: (A B C); ((A B C) NIL); ((A B C)) 2-6: (HAMMER SCREWDRIVER) (PLIERS HAMMER SCREWDRIVER) (HAMMER SCREWDRIVER) (PLIERS HAMMER SCREWDRIVER) (PLIERS HAMMER SCREWDRIVER) (SAW WRENCH PLIERS HAMMER SCREWDRIVER) (PLIERS HAMMER SCREWDRIVER) (SAW WRENCH PLIERS HAMMER SCREWDRIVER) (SAW WRENCH PLIERS HAMMER SCREWDRIVER) 2-7: (NIL) 2-8: 3; 3; 1; (ARISTOTLE SOCRATES PLATO) ((ARISTOTLE) (SOCRATES) (PLATO)) ((PLATO SOCRATES ARISTOTLE)) 2-9: 3; ((CEREAL WHEATIES) (DRINK COKE) (CAR CHEVROLET)) ((CAR CHEVROLET) (DRINK COKE) (DRINK COKE) (CAR CHEVROLET)) 2-10: 2; 15; 4