CS 312 Project: Haskell

Due: 11:59 PM, Nov 20
Project: haskell 

Purpose

The purpose of this assignment is to learn something about functional programming. This is couched in the guise of writing solver for knight placement.


Knight Placement Problem

chess  

The input to a knight placement problem is a chess board with a number of red queens already placed. The goal is to find all safe positions to place a black knight. A position is safe if neither any of the red queens can immediately capture the knight, nor the knight can immediately capture any of the red queens. Specifically, the knight cannot be in the same column or row or diagonal as any of the red queens is. Meanwhile, no red queen can appear in any of the cells that the knight can immediately jump to. Certainly, no two pieces can be put into the same cell.

The left graph shows some examples. The placement at the top is unsafe because the knight can be captured by the left queen, and the rightmost placement is unsafe either because the right queen can be captured by the knight. The leftmost placement is safe.


How to run

You must name your Haskell program "knightPlace.hs". It must contain a function named "knightPlace" such that when it is called correctly, it outputs a list containing all the safe placements of the knight. The ith element in the list itself is a list, containing the row numbers of all safe placements in the ith column of the chess board. Column and row numbers are 1-based; "[0]" means no legal placement in that column.

Your program will be tested in the interactive mode of Haskell. Below we show how the program is invoked and the correct outputs. The first command loads the Haskell file; the second command invokes your program on the example placements as shown in the graph above, and the third command invokes the program on another set of queen positions on a 4x4 chess board. The size of the chess board is determined by the length of the input list. The input list indicates the positions of red queens. The ith element in it indicates the row number of the queen in the ith column; again, "0" means no queen in that column.You can assume that there is no more than one queen in each column. Your program should be able to handle an input list of an arbitrary size.

> :l knightPlace

> knightPlace [0, 0, 6, 0, 0, 4, 0, 0]

[ [1, 2, 3], [1, 2, 3], [0], [1], [1], [0], [1, 7, 8], [7, 8]]

> knightPlace [4, 0, 4 , 4]

[ [0], [1], [0], [0]]


Haskell compiler

We will test your program on the CS department Linux machines through the GHC compiler for Haskell. The GHC compiler is already available on those machines as I demonstrated in our class. If you still want to code on your Windows machine, you may follow this link to install GHC. In any case, you need to make sure that your code works on department Linux machines.


Hint

Chapter 14.3.10 in our recommended textbook has an example that may give you some useful hints. There are two typos in the example code. Here is a solution that works with our version of Haskell. You can find more materials on that chapter on the Blackboard.


Borrowed from Xipeng Shen
April 6, 2015