CIS 554 Assignment 4: Mastermind
Fall 2012, David Matuszek

Purposes of this assignment

General idea of the assignment

Play a game of Mastermind. The computer creates a random pattern, and the human user tries to discover it.

You can play this game online at More detailed instructions are at

To get the maximum benefit from this assignment,

While there are exceptions, concise code is almost always simpler and better than verbose code. Higher order functions (once you are familiar with them) are almost always easier to read than recursive code.

Detailed specification

Provide a function main (with no arguments) to start the program.

Ask the user whether he or she wants to play a game. As possible answers, accept:

We will use:

After each move input by the user, the program should print out every move made so far in this game, and how it was scored, most recent move first. For example, if the player has just entered vooy, the program might display

v o o y   K K W
o v v y   K K W
o r y o   W W
r y g b   W

(where the actual pattern is y o v y).

Be sure to congratulate the user when he/she finds the solution!

State and randomness


A law of functional programming is that when you call a function with the same parameters as before, you get the same result as before. This obviously isn't what you want from a random number generator! Therefore, you need to isolate the randomness in the imperative part of the program: the IO monad. is probably all you need to know about random numbers in Haskell; if not, you can find a lot more at


For the same reason, you cannot manipulate an external "state" in a function. All the information the function can access is provided by the parameters. Therefore: The entire "state" of the program must be represented by the values of one or more parameters.

In this assignment, for example, the "state" that you need is the sequence of moves and scores made by the player, along with the correct pattern. Functional programming encourages minimizing the amount of state that needs to be carried around.


While you can do quite a bit of work in the imperative IO monad, this is the equivalent of trying to write a Java program in Haskell. It is basically fighting with the language, instead of letting the spirit of the language guide you. Try to write Haskell code in Haskell, not Java code.

Due date

Your program is due before 6am Friday, October 26, submitted via Blackboard. Usual late penalties will apply.