Math 372 Homework 1 Lisp Dordal Due: Wednesday Sept 22, 1999 Write the following functions in Lisp. Sumcubes should use ordinary arithmetic recursion; the others should use cdr recursion. I recommend you use the Lisp+IDE option, available in e:\lisp and *soon* on the start menu; the IDE contains a helpful edit window. Note that if you edit stuff there, you still have to save that and load your work into the Debug (Lisp) window, or else transfer it with cut-and-paste. I'm still working on the most convenient way for you to turn in your work; one way is to scroll back in the Debug window, select everything, and use copy/paste to transfer it to some text editor window. As far as editing lisp is concerned, I suggest typing your functions in a Lisp IDE edit window (notice how this editor gives a visual indication of matching parentheses), and always save into a file even if you do use cut-and-paste to get your work to the Debug window. sumcubes: This should take a number, n, and return the sum 1*1*1 + 2*2*2 + ... + n*n*n. Thus, (sumcubes 0) returns 0, and (sumcubes 4) returns 100 (=1+8+27+64). sumlist: This should take a list and return the sum of all the numbers in the list, at the top level. Thus, (sumlist '(1 2 (3) (4 a) nil 5)) should return 1+2+5=8; the numbers 3 and 4 are not at the top level. pairup: This should take two lists of the same length and pair up the corresponding entries into a list of two-element lists. Thus, (pairup (a b c) (1 2 3)) should return ((a 1) (b 2) (c 3)). This function will need cdr recursion on both parameters; if the parameters are x and y then the recursive call should include something like (pairup (cdr x) (cdr y)) i.e. this involves cdr recursion on both parameters. You will also, of course, have to form the list of (car x) and (car y) [use (list (car x) (car y))], and stick this onto the front. Try to avoid using the length function to see if the lists are the same length. In the event that the lists are not the same length (and using cdr recursion you will discover this when one of the lists is nil and the other isnt), you can use the function ERROR to cause an exit. Specifically, use of the following (error "pairup: lists of unequal length") will terminate your function. Unfortunately, it will also start the debugger; you may have better luck with ABORT, and also some mechanism for generating a line of output. Note that errors discovered deep within the recursion are particularly difficult to handle gracefully without some such mechanism. countwhat: This should take a list and a predicate, and return the count of the number of elements of the list that satisfy the predicate. Here is, for comparison, a function that returns a list of the things that satisfy the predicate: (defun filter (lis pred) (cond ((null lis) nil) ;; nobody satisfies pred. ((funcall pred (car lis)) (cons (car lis) (filter (cdr lis) pred))) ;; note use of pred and funcall! (t (filter (cdr lis) pred)))) Your examples should include (countwhat '(a 1 c 2 b e) #'numberp) and also the same list with the predicates #'symbolp and #'atom. You should also give another example with a different, hierarchical, list (e.g. (a (b) c (2 3) 4)). #'fname is the same as (function fname) and is preferred in theory to 'fname (or (quote fname)).