CSE 5326 - Symbol Manipulation Languages HW #1 - Common LISP Primitives and Recursive Functions Due: Wednesday 9/18, in class Instructions: Using a separate sheet of paper, write (legibly) the answers to the following questions. Answers requiring you to write Common LISP must be written in the Common LISP dialect using only the functions covered in class. If I ever get you accounts, you're welcome to try your solutions for real; however, the questions are easy enough to do without the computer. 1. Draw the box-and-pointer representation for the following list: ((A B) C D (E (F G) H) I J (K)) 2. Assuming X is a symbol whose value is the above list, show the result of evaluating the following Common LISP expressions: a. (FIRST X) b. (FIRST (REST (REST (REST X)))) c. (FOURTH X) d. (SECOND (FOURTH X)) e. (TENTH X) f. (CONS (SECOND X) (TENTH X)) g. (LIST (SECOND X) (TENTH X)) h. (APPEND (FIRST X) (SECOND (FOURTH X))) i. 'X j. (EQUAL (FOURTH X) (LIST 'E (APPEND (LIST 'F 'G) (CONS 'H NIL)))) k. (COND ((EQUAL (FIRST X) 'C) '(C IS NUMBER 1)) ((EQUAL (SECOND X) 'C) '(C IS NUMBER 2)) (T '(C IS BEYOND 2))) 3. Write a Common LISP expression to build the list in problem 1 using only the function CONS. 4. Write a single recursive function for computing the length of a list. You may assume the argument to your function is a list. For example, > (LENGTH NIL) 0 > (LENGTH '(A B C)) 3 > (LENGTH X) ; note LENGTH should count only top-level elements of 7 ; its list argument 5. Rewrite your LENGTH function to use tail recursion. You should need no more than two functions to accomplish this. 6. Briefly compare the space and time complexity of your two functions from problems 4 and 5 in terms of the length of the input list. If you wrote the functions correctly, the tail-recursive version should have a better space complexity. See Sussman and Abelson, Section 1.2.3 for a discussion of function complexity. 7. The winner in the game of tic-tac-toe is the first player to get three of the same mark (X or O) in a row, column, or diagonal of the grid below: 1 | 2 | 3 ---+---+--- 4 | 5 | 6 ---+---+--- 7 | 8 | 9 If all grid spaces are filled without "three in a row" for one player, then the game is a tie. Your job is to write a function called TTT that will return X, O, or TIE, given the grid entries in a list in the following sequence: (1 2 3 4 5 6 7 8 9). For example, >(TTT '(X X X O O X O X O)) X >(TTT '(X X O X O X O X O)) O >(TTT '(X O X X O O O X X)) TIE Remember to use Common LISP, and only the functions covered in class. ÿ