Comp 372: Programming Languages Program 2 Dordal Sept 27, 1999 Due: Oct 6, 1999 Write the following functions in lisp. Do not use any global variables! 1. SUM2 finds the sum of all the numbers at any level of a list. Thus, (SUM2 '(1 (a) (b 2) 3 ((4) 5))) would return 1+2+3+4+5=15. Note that SUMLIST would return just 1+3=4 here. Use car/cdr recursion. SUM2 should work on atoms too; (SUM2 6) => 6 and (SUM2 'A) => 0. 2. Write SUMLIST2, to do the same thing as SUMLIST from homework 1, but implemented using a DO loop instead of cdr recursion. Note that SUMLIST2 just adds up numbers at the "top level" of a list; SUM2 (above) adds up numbers appearing at any level of nesting. Write the following functions **without loops or recursion**; use MAPCAR or its relatives (including FILTER; the built-in version is called REMOVE-IF-NOT) to do the iteration, and an appropriate lambda function. 3. MYINTERSECT. This should take two lists, and return the list of all elements common to both lists. For example, (MYINTERSECT '(a b c) '(a c d e)) => (a c). The builtin function MEMBER may be useful. 4. MYUNION. This should take two lists and return the list formed by appending to the second list all the elements from the first that do *not* appear in the second. Thus, (MYUNION '(a b c d) '(b d e f)) => (a c b d e f). Hint: use FILTER/REMOVE-IF-NOT to get the list of elements to be added, and then APPEND. Or use MAPCAR and LET as follows: create a LET variable named RESULT, initialized to the second list, and use a lambda function that invokes (SETF RESULT (CONS X RESULT)) whenever X is *not* a member of the second list. Then return RESULT at the end. 5. SUMLIST3. Again, this should return the same thing as SUMLIST from hwk1, or SUMLIST2 above. Hint: keep a the running sum in a LET variable. You *may*, if you wish, use DOLIST, but if so your program should have no explicit SETFs.