kbushnel.sdf-us.org/contact.html Week 3 comments, questions Course: An Introduction to Common Lisp Instructor: jgw Book: "COMMON LISP: A Gentle Introduction to Symbolic Computation" by David S. Touretzky copyright 1990 ANSWERS to questions assigned =================================== 7.2 Let the global variable DAILY-PLANET containt the following table: ((olsen jimmy 123-76-4535 cub-reporter) (kent .... etc. etc. ... Use MAPCAR on this table to extract a list of social security numbers. Answer: (MAPCAR #'third DAIly-PLANET) 7.7 Write a function that takes a list such as (UP DOWN UP UP) and "flips" each element, returning (DOWN UP DOWN DOWN). Your function should include a lambda expression ... Answer: >(defun UP-DOWN (n) (OR (if (equal n 'UP) 'DOWN)(if equal n 'DOWN) 'UP))) >UP-DOWN >(mapcar #'UP-DOWN '(UP DOWN UP UP)) [I couldn't figure out how to use LAMBDA. Tried (mapcar #'lambda UP-DOWN '(UP DOWN UP UP )), etc.] 7.14 Show how INTERSECTION and UNION can be written using REMOVE-IF and REMOVE-IF-NOT. (defun my-intersection (x y) (remove-if-not #'(lambda (e) (member e y)) x)) (defun my-union (x y) (remove-if #'(lambda (e) (member e x)) y))) [???? doesn't work] try (defun my-union (x y) (append x (remove-if #'(lambda (e) y (member e x)) y))) 7.24 What is an applicatie operator? Answer: a FUNCALL 7.25 Why are LAMBDA expressions useful? Answer: Because they hide a function which makes them invaluable where two or more values are called and one must be defined by a lambda function. They're a pain-in-the-a#'#' though. 7.30 ... write an expression to return a trilingual dictionary ... Answer: (mapcar #'(lambda (x y) (list x (last y))) '((one un)(two deux)(three trois)(four quatre)(five cinq)) '((one uno)(two dos)(three tres)(four quatro)(five cinco)) QUESTIONS about reading material ================================= pg 210, last line: (remove-if-not #'oddp '(2 0 -4 6 -8 10)) [=>] NIL Does that mean 'remove-if-not' returns a NIL if no conditions are met? So would: (remove-if-not #'oddp '(2 0 -4 6 3 8 10)) => 3 be right (be T)? on pg 211, if the above output is 'NIL' should the following be NIL instead of 0: >(defun count-zeros (x) (length (remove-if-not #'zerop x))) >(count-zeros '(1 2 3 4 5)) => 0 It's a predicate, and there are no zeros and in the above example there are no odd numbers. What's the difference? COMMENTS ================================= pg 206 "Throughout this book we will refer to the objects you get back from a #'(LAMBDA ...) expression as lexical closures." [Pretty important, don't you think?] This LAMBDA thing looks like one of the more important concepts covered in the book. If anybody has brief sentence or two description to throw in I'd find it helpful. I see a Lambda function as a predefined function without a handle. Ahem! A 'lexical closure' without a close.