Due: Feb. 12, 2009 CS 480 Spring 2009 Dr. Duric Homework #1 1. (4p) Give iterative and recursive definitions of a function that (a) takes a positive integer and prints that many dots. (b) takes a list and returns the number of times the symbol a occurs in it. 2. (3p) A friend is trying to write a function that returns the sum of all the non-nil elements in a list. He has written two versions of this function and neither of them work. Explain what is wrong with each, and give a correct version: (a) (defun summit (lst) (remove nil lst) (apply #'+ lst)) (b) (defun summit (lst) (let ((x (car lst))) (if (null x) (summit (cdr lst)) (+ x (summit (cdr lst)))))) 3. (3p) Suppose the function pos+ takes a list and returns a list of each element plus its position: > (pos+ '(7 5 1 4)) (7 6 3 7) Define this function using (a) recursion (b) iteration (c) mapcar 4. (3p) Define a function f that takes one numeric argument, and returns the greatest argument passed to it so far: >(f 5) 5 >(f 2) 5 >(f 10) 10 5. Tree traversals (6p): Write 3 functions that can be used to traverse and linearize binary trees: inorder (left, root, right), preorder (root, left, right), and postorder (left, right, root). Each of the functions should take a binary tree as input and return a flat list that corresponds to the traversal implemented by the function. You can represent binary trees as either nested or assoc lists. Nested (recursive) representation: (root (left subtree) (right subtree)) Assoc list representation: ((node1 left-child1 right-child1) (node2 left-child2 right-child2) .... ) where node1 is the root of the tree. Examples: (setq Tree1-nested '(1 (2 (3 4 5) (10 11 12)) (6 () (7 () 8)))) (preorder-nested Tree1-nested) should return (1 2 3 4 5 10 11 12 6 7 8) (inorder-nested Tree1-nested) should return (4 3 5 2 11 10 12 1 6 7 8) (postorder-nested Tree1-nested) should return (4 5 3 11 12 10 2 8 7 6 1) (setq Tree1-assoc '((1 2 6) (2 3 10) (3 4 5) (10 11 12) (6 nil 7) (7 nil 8))) (preorder-assoc Tree1-assoc) should return (1 2 3 4 5 10 11 12 6 7 8) etc. 6. Dictionary (3p): You are given a dictionary in the form of "exploded" symbols (e.g. (d e f u n), (s e t q), (s e t f)). Write a recursive function, lookup, that takes a prefix of an exploded symbol (e.g., (s e t)) and a dictionary and returns the list of all items in the dictionary that match the prefix. 7. Occurrences (3p): Define a function that takes a list and returns a list indicating the number of times each (eql) element appears, sorted from most common element to least common: > (occurrences '(a b a d a c d c a)) ((A 4) (C 2) (D 2) (B 1)) Instructions for submission: ---------------------------- (1) Using script or dribble, you are to capture the output of a Lisp session in which you successfully load and execute your code, showing sufficient testing of your function(s). (2) a. In class hand in a hardcopy report consisting of: - a cover sheet containing: Your name CS480 Spring 2009 Dr. Duric Homework #1 (A short summary stating whether you were successful, describing any problems, etc.) - a print out of the Lisp code. - a printout of a captured Lisp session involving the execution of the HW1 Lisp code. b. Send a SINGLE email to zduric@cs.gmu.edu formatted in the following way: - the subject field of the email should read: CS480 HW#1 Duric - the content of the email should be a commented version of the Lisp code for HW1. Comments at the top should include: Your name CS480 Spring 2009 Dr. Duric Homework #1 As a safety precaution, always CC yourself when you submit homework this way and keep it around until it has been graded and returned.