CISC 280 Programming Paradigms, Fall 1996 Last homework assignment. Due date is Wed, Dec 11 at 5pm. **************************************************************** First problem (20 points): Exercise 3.41 The code in the problem statement refers to stream functions named 'singleton and 'enumerate-interval. These functions are defined in ~saunders/280/streams.scm. The definitions in this file will be put in with the utilities which are automatically loaded when scm starts up. The code also uses a stream special form, 'collect. You are to replace that use of collect by equivalent code. See page 257. [Ambitious people may wish to try implementing collect using defmacro. See streams.scm for a sample use of defmacro. To do this is definitely optional. If you do this you do not need to do the version without using 'collect.] The key thing in solving the problem is 1. to choose your representation of boards partially populated by queens and then 2. implement 'empty-board, 'safe?, and 'adjoin-position. To do this you will have to choose an internal representation of a n by n chess board partially occupied with queens. It will always be the case that the k queens on the board are in the first k columns, so the simplest representation I can think of is to have the representation be a list of k row indices. Thus '(2 7 3) would represent the board with a queen in row 2, column 1 and a queen in row 7, column 2, and one in row 3, column 3). I will suggest another representation later. empty-board --> the representation of a board with no queens on it. For the representation system above, (). (safe? row col board) --> boolean. Returns #t if and only if no two queens would be attacking each other if a new queen in position (row, col) were added to board. You can assume that board has queens in columns 1 thru col-1, and that no two of THESE queens attacks each other. Thus the only question is whether any of them attacks position (row,col). (adjoin-position row col partial-board) --> fuller-board, where it is assumed that partial-board is a representation of a board partially populated by queens. fuller-board becomes the representation of a board which additionally has a queen in the (row, col) position. a queen in position (i,j) attacks a position (r, c) if one of the following 4 things is true. 1. i = r, (positions in same row) 2. j = c, (positions in same column) 3. i+j = r+c (positions are on same neg slope diagonal) 3. i-j = r-c (positions are on same pos slope diagonal) A suggestion: To make the work easier in 'safe? It would be useful to have the board representation store three lists, namely (1) the rows of the k queens, (2) the neg diagonals of the k queens, and (3) the pos diagonals of the queens. There is no need to store the cols of the k queens, since that is implicitly columns 1 thru k. Also k is simply the length of any of the lists. Thus for the three queen example used above, the representation would be '((2 7 3) (3 8 6) (1 5 0)). Submit 1. a printout of your code. 2. a test run including some computation on the result stream to reveal at least one or two of the solutions and to reveal the number of solutions (the length of the stream of solutions). Extra credit (5 points) - for writing a tool to display the board, say as an 8 by 8 character grid with x's marking queens and .'s marking empty spots (or blanks, but with a border around all). **************************************************************** Second problem (10 points): Exercise 3.42 **************************************************************** Third problem (15 points): Exercise 3.46 Submit 1. a printout of your code 2. a test run. define additional functions and/or do additional evaluations so as to show (a) the first 10 elements of the "2-3-5" stream, and the number of "2-3-5" numbers less than 1000. Extra credit (3 points): Do the same for "3-5-7" numbers. **************************************************************** Fourth problem (extra credit problem -- tough: 20 points). Write a program to solve this problem: There are two integers x,y between 2 and 100, inclusive, and two people P,S. P is told the product x*y. S is told the sum x+y. Neither is told the specific values of x,y. P,S have the following conversation: P: I cannot determine x,y. S: I already knew that. P: In that case, I now know x,y. S: In that case, I too now know x,y. What are x,y?