Week 5 comments, questions Course: INTRODUCTION TO LISP instructor: jgw ANSWERS to questions assigned = 8.2 ============================== Q:8.2 Show how to write ANYODDP using IF instead of COND. Answer: (defun anyoddp (x) (if not (null x)) (if (oddp (first (rest x)))t))) [Wrong answer. Can't figure out how to iterate through a list.] Book answer: (defun anyoddp *x) (if x (if (oddp (first x)) (first x) (anyoddp (rest x))))) Comments: Is (anyoddp (rest x) the recursive part of the ANYODDP function using IF? ========== end 8.2 ==================== = 8.4 =================================== Q:8.4 Here is a skelton for a LAUGH function: (defun LAUGH (n) (cond (alpha beta) (t (cons 'ha gamma)))) Q pt1: Under what condition should the LAUGH function stop recursing? Answer: zerop Q pt2: What value should LAUGH return for that case? Answer: () Q pt3: Fill in alpha beta gamma: Answer: (defun LAUGH (n) (cond ((zerop n)()) (t (cons 'HA (LAUGH (- n 1)))))) Q pt4: What happens to LAUGH 0? Answer:Empty list is not displayed? Q pt5: What happens to LAUGH -1? Answer: Doesn't exist with this solution. ===== end 8.4 ============================ = 8.11 =================================== Q:8.11 Write a correct version of of the FIB function [allowing for FIB(0) and FIB(1)]. Answer: (defun FIB (n) (cond ((equal n 0) 1) ((equal n 1 )1) ((NOT (equal n 0) (+ (fib (- n 1)) (fig (-n -2)) )))) ====== end 8.11 ==================== = 8.15 ============================= Q:8.15 Circular list. What is the CAR and the CDR? What will the COUNT-SLICES function do when given this list as input? Answer: The CAR is 'X' the CDR is the CAR. The COUNT-SLICES function will run out of memory. ===== end 8.15 ===================== ====================================== ====================================== ===================================== misc questions -- suggested discussion. ------------------------------------------------------------------------------------------------- 1) Are sequences just allowed with the COND function or are there any other functions that can process sequences? IF function? Any others? --------------------- 2) What is the purpose of 'T' in the first three recursive examples of the book, chp 8, i.e ...(t (ANYODDP (rest x) ... ...(t (* n (FACT (-n 1) ... ...(t (+ 2 (COUNT-SLICES (rest loaf) ... Does calling the name of the functions (ANYODDP, FACT, COUNT-SLICES), in these three statements, make the function capable of processing sequences? --------------------------- 3) Is recursion a part of other distributed programming languages? ======================================= comments ------------------------------------------------------------------------------------------------------------- 1) From the book "Common Lisp an Interactive Approach" by Stuart Shapiro Every recursive function is of the form (defun fn varlist (cond cond-pairs)) or of the form (derfun fn varlist (if test then else)) ---------------------------------------------------------------------------------------------------------- Please edit function list definitions at: http://www.programsplus.us/code/Lisp/functions/functionList.html Double click definition to edit (html5). Save a copy for your records. Thank You kbushnel.sdf-us.org/contact.html