Complete this assignment with the same team you worked with for Substitution (Written). You and your partner should each understand the answers to all the problems, so don't just split up the work.For this assignment, we will use the Haskell programming language. The Glasgow Haskell Compiler (GHC) is freely available for most platforms and is installed on the CS department network. To start the interactive evaluator, simply run ghci.
Haskell has wonderful standard libraries which probably contain the function you're looking for. To see tree-style browsable docs, check out the Haskell Hierarchical Libraries . You should particularly take advantage of the
Prelude, which is the set of functions that are "built in" to Haskell. You're welcome to use other functions; if you want to do so, you must put an
importstatement at the top of your code (there's an example in the template).
The following are library functions that you may find particularly helpful, and which are all in the Haskell
|Haskell Function||Scheme Analog|
|<, >, <=, >=||<, >, <=, >=|
|take||(returns a prefix of a list)|
|takeWhile||(returns the prefix of a list satisfying a predicate)|
Any function whose name begins with a symbolic character is an infix operator. Other binary functions can be used as infix operators by enclosing in backquotes (e.g.,
x `mod` y). Also, infix operators can be used as ordinary functions by enclosing them in parentheses (e.g.,
(!!) [1, 2, 3] 2).
Please submit a README file explaining how you tested your code. Actual test cases are preferable, but a clear summary of what you did will be sufficient as long as your code actually works as you claim it does.
Problem 1: Prime Numbers
isPrime :: Integer -> Bool, which determines whether a given integer is prime.
primes :: [Integer], the list of all primes.
isPrimeso that it only tests divisibility by prime factors. Just turn in the revised version.
Problem 2: Longest Common Subsequence
buildList :: Int -> (Int -> a) -> [a], where
((buildList n f) !! i) == (f i)(for all i in [0 .. n-1]).
buildTable :: Int -> Int -> (Int -> Int -> a) -> [[a]], where
(((buildTable n m f) !! i) !! j) == (f i j)(for all i in [0 .. n-1], j in [0 .. m-1]).
lcsLength :: String -> String -> Int, which quickly computes the length of the longest common subsequence of two strings s1 and s2. Hint: you can easily compute
lcsLength (take i s1) (take j s2)from
lcsLength (take (i-1) s1) (take j s2), lcsLength (take i s1) (take (j-1) s2), and lcsLength (take (i-1) s1) (take (j-1) s2).
and the knowledge of whether
(s1 !! i) == (s2 !! j). Note that you will lose credit if you use a slow, "brute force" method; make use of your table!
Problem 3: Minimax SearchIn this exercise, you will implement a strategy to play a simple game. The game is called Mancala, but you won't need to worry about the specific rules, since we have implemented that part for you. Your job is to build a tree of possible move sequences and choose the move that appears best.
The support code below provides the following set of data types and functions:
Player: values of this type represent the players of the game (
State: values of this type represent game configurations.
initialState :: Player -> Staterepresents the initial configuration of the game board (the given player goes first).
getPlayer: State -> Player: given a configuration, returns the player who makes the next move.
gameValue :: State -> Intreturns Player A's score minus Player B's score. (Player A wants a big value and Player B wants a small value.)
nextStates :: State -> [State]gives the possible configurations after the next move. If the returned list is empty, then the game is over.
simulateGame :: IOuses an IO loop to allow you to play Mancala. The valid moves are the numbers 1 through 6 and they indicate the player playing the nth cup on her side. Player A goes first; her cups are on the top row and are numbered from right to left. Player B is the bottom row numbered from left to right.
simulateAIGame :: (State -> Int) -> IOconsumes a scoring function and plays the game where Player A is the AI using that function. We give you the helper function,
level :: Int -> IOin the stub file, which consumes a difficulty and runs simulateAIGame using your functions. This may be helpful for testing.
- Define a datatype
GameTreeto represent the game state after any sequence of moves. Each node should have its current configuration and a list of trees, where each tree corresponds to the game states obtainable after making any one legal move.
mkTree :: State -> GameTree, which consumes a state and returns the tree of all legal board configurations (those obtainable by repeated application of
nextStatesto the given state). Use this to define
mancala :: GameTree, the entire game of Mancala as a GameTree (remember that Player A goes first).
prune :: Int -> GameTree -> GameTree, which prunes a tree to a given depth.
minimax :: GameTree -> Int, which consumes a (pruned) tree and evaluates the configuration by looking ahead and applying the minimax algorithm. If a node has no children, its value is the
State. Remember, Player A, Maxi, is always trying to get the maximum value, and Player B, Mini, is always trying to get the minimum value.
Note that you must download both files (even for problems 1 and 2), and
you must put them both in the same directory. To test your code interactively,
cd into the directory with your code, and then do
ghci Laziness.hs at which point you should either see a parse
error, type error, or this:
___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base ... linking ... done. [1 of 2] Compiling Game ( Game.hs, interpreted ) [2 of 2] Compiling Laziness ( Laziness.hs, interpreted ) Ok, modules loaded: Game, Laziness. *Laziness>
ghcisupports tab completion, which is a very handy feature.
A Haskell stub file for the problem set: Laziness.hs.
The support code for Problem 3: Game.hs. Remember, you need neither read nor understand this code.
From the directory containing the files for the assignment you wish to hand in, execute
/course/cs173/bin/cs173handin lazinessEach partner should submit the code files for the implementation and a readme. The readme file should contain the names of the members of the group.