CS 580 Spring 2001, Jana Kosecka, HOMEWORK 2, due February 1st
(30 points)

 
1. (6p) 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) 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

3. (6p) Write a function called MY-REMOVE which removes all occurrences of an
   element from a list. MY-REMOVE should use recursion. Here are a
some examples of how your function should behave:

            >(my-remove 'hello '(hello why dont you say hello))
            (WHY DONT YOU SAY)
            >(my-remove '(oops my) '(1 2 (oops my) 4 5))
            (1 2 4 5)
 

4. (5p) 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 (10p):
   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 nil) (6 nil 7) (7 nil 8)))
    (preorder-assoc Tree1-assoc)    should return (1 2 3 4 5 10 11 12 6 7 8)
    etc.
 
 
 

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). On osf1 type script and your session
    will be saved in a file typescript, whose hardcopy you will hand in.
    To exit script simply type 'exit'.

(2) a. In class hand in a hardcopy report consisting of:

       - a cover sheet containing:

         Your name
         CS580 Spring 2001, J. Kosecka
         Homework #2

        (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 HW2 Lisp code.
 

    b. Send a SINGLE email to clevcovi@cs.gmu.edu formatted in the
       following way:

       - the subject field of the email should read: CS580 HW#2  Kosecka

       - the content of the email should be a commented version of the
         Lisp code for HW2.  Comments at the top should include:

         Your name
         CS580 Spring 2001, J. Kosecka
         Homework #2
         As a safety precaution, always CC yourself when you submit homework
         this way and keep it around until it has been graded and returned.