From UBC Wiki


Authors: Shannon Lawrence, Anthony Mak

What is the problem?

Creating a Tic-Tac-Toe game implementation using Haskell.

What is the something extra?

- Having different types of players. We had a simple player who always played the same actions, and a random player who played randomly, and then for the something extra we incorporated the Mini-Max algorithm.

What did we learn from doing this?

One challenge we had was trying to import System.Random, which was solved by using stack ghci to first import it:

brew install haskell-stack

stack ghci --package random

import System.Random

In conclusion, using Haskell to design a game such as TicTacToe is generally quite feasible and efficient for several reasons. One is the functional nature of the Haskell language, which lent us referential transparency, meaning that each expression is always guaranteed to return the same result, which makes it easy to deduce/prove a function is correct. Because of this, none of the code we wrote could affect the state of the computer or anything, which made it less prone to errors, and because the data was immutable, our program could also be more brief and clear. This is the meaning of Haskell having no side effects, which was extremely helpful.

Using Haskell was also much simpler for this task than using an object-oriented language, for example, which would have required us to make a bunch of separate classes for Game, Board, Pieces, and Players etc.

The interactive development aspect of Haskell also made it easier to experiment and test our code interactively as we worked on, and being able to see results immediately using the ghci interactive environment as we coded was helpful. Another perk of using Haskell was strong typing, which allowed us to probably eliminate many many bugs as these errors were caught at compile time instead of after.

We also learned first-hand how using lazy evaluation in Haskell saves runtime and makes our program more efficient.

However, Haskell would be quite hard for incorporating components like machine learning and AI.

Links to code etc