Course Grades Email
Welcome
Policies Grades Inc Intgrty Preface Part I Chap 1 Chap 2 Chap 3 XEmacs Chap 4 Chap 5 Chap 6 Chap 7 Chap 8 Chap 9 Part II Chap 10 Chap 11 Chap 12 Chap 13 Chap 14 Chap 15 Chap 16 Chap 17 Chap 18 Chap 19 Chap 20 Chap 21 Chap 22 Chap 23 Part III Chap 24 Chap 25 Chap 26 Chap 27 Chap 28 Chap 29 Chap 30 Chap 31 Chap 32 | CHAPTER 10: DEFINING YOUR OWN FUNCTIONS - Notes
-
- Read Chapter 10 along with the following
- p. 64
- Defining
list3 is still a good example for the discussion in the text, but I decided not to put off telling you about the function list till Exercise 10.12, and told you about it, instead, in the notes to Chapter 8. So now you should realize that (list o1 o2 o3) is a much easier way of expressing (cons o1 (cons o2 (cons o3 '()))) . - p. 65-66
- Similarly, you don't really need to define
list2 since list is available. Study this material in the text anyway for what it tells you about programming in Common Lisp. - Do Exercise 10.1, but instead of
list3 , use average3 as given here (defun average3 (x y z) "Returns the average of the three numbers X, Y, and Z." (/ (+ x y z) 3.0)) Notice: - XEmacs indents nicely whenever you type a newline in the middle of your function definition;
- since you are typing into an editor, you can edit the function definition at any time before completing it;
- if you complete the function definition, but made a mistake, you can use
C-c C-p to yank it back and edit it (The definition that is in effect is the last one you entered.); - if you type a newline from anywhere inside a completed S-expression, Lisp will evaluate it.
- Explore the "ACLHelp" menu. With your curser on or just after any occurrence of the symbol
average3 in your *acl* buffer, try each of the items in the "ACLHelp" menu. Notice the keyboard shortcuts for these 4 menu items. (You needn't use the shift key for C-c a , C-c d , or C-c f .) Try these 4 menu items with previously defined Lisp functions such as cons . Try them with symbols that don't name functions. - Do Exercise 10.2, but check the values of
x y and z both before and after a call to average3 . - You are to do Exercise 10.3, but instead of using
switch and list2 , use median4 as shown here (defun median4 (list) "Returns the median of the numbers in the LIST, assuming that LIST contains 4 numbers in order." (average2 (second list) (third list))) and average2 . Define median4 and try it out before defining average2 in the same way that the Exercise tells you to try switch before defining list2 . You should get an error message and be in the debugger. The text asks you to find out how to determine the value the lambda variable is bound to while in the debugger. The technique for acl in XEmacs is: - Under the "ACLDebug" menu, choose the "Debug process" item, or type
C-c s . Accept the suggestion to debug the "Initial Lisp Listener" by typing a carriage return in the minibuffer. - Notice that you get information in a "*debugger:Initial-Lisp-Listener*" window, and an arrow (
-> ) points to the form you evaluated that caused the error. - Put your cursor on that arrow and type the letter
l . - You will get a temporary window in which you will see the binding of your lambda variable.
- Get rid of the temporary window by typing
C-space as it suggests. - Back in the debugger window, type a
q to return to the *acl* window. - Back in the
*acl* window, you may enter C-c C-d to return to the top-level listener. - Replacement Exercise 10.4: Now define
average2 as a modification of my average3 above, and test median4 again. - Do Exercise 10.5, but trace the functions
median4 , second , third , and average2 while evaluating (median4 '(1 2 3 4)) . Note you can also turn tracing on and off by putting your cursor on or just after a function name, and choosing the "Toggle trace" item under the "ACLDebug" menu or typing C-c t (trace) with no arguments shows you all the functions being traced. With one or more arguments, it add those functions to the ones being traced. (untrace) turns tracing off on all functions being traced. With one or more arguments, it turns off tracing on just those functions. - Do Exercises 10.6, but use
median4 instead of switch . - Do Exercises 10.12 and 10.15 if you feel you need to.
- Do Exercises 10.14.
- Do Exercises 10.16 and 10.17.
- Create a file named
ch10.cl Paste the following into it: ----------------------- cut here ----------------------- ;;; Answers to Chapter 10 Exercises ;;; ;;; 10.4 The definition of average2 is ??? ;;; 10.14 The definition of sqr is ??? ;;; 10.16 The definition of reverse3 is ??? ;;; 10.17 ;;; Common Lisp has the functions first, second, third, up through and including ??? ----------------------- cut here ----------------------- Replace the question marks in ch10.cl with the function definitions you wrote for the given Exercises 10.4, 10.14, and 10.16. Replace the question marks for Exercise 10.17 with the name of the last function in the series. Use these functions whenever they're useful. Submit ch10.cl , and go on to Chapter 11.
|