Haskell Traffic Light System Tutorial

← Go Back Tutorials
565 Words • ~3 Minute Reading Time
Haskell Traffic Light System Tutorial by Eric David Smith

Haskell Traffic Light System Tutorial


In this tutorial, we'll build a traffic light system using Haskell, simulating a real-world scenario for a city. We'll define the traffic light states, create functions to transition between states, and write tests to verify our system.

Prerequisites


Haskell Platform


You'll need the Haskell Platform, which includes the Glasgow Haskell Compiler (GHC), the Cabal build system, and other essential tools for developing in Haskell. You can download it from the official website.

Text Editor or IDE


You'll also want a text editor or integrated development environment (IDE) that supports Haskell. Some popular options include:

  • Visual Studio Code with the Haskell extension (I personally use VSCode for all my Haskell development)
  • IntelliJ IDEA with the HaskForce or IntelliJ-Haskell plugin

Choose the one that suits your preference and comfort level.

Basic Knowledge of Haskell


While this tutorial provides a step-by-step guide, having a basic understanding of Haskell's syntax and concepts will be beneficial. If you're new to Haskell, you might want to explore introductory materials or tutorials to familiarize yourself with the language.

Step 1: Define the Traffic Light States


We'll begin by defining the data type for our traffic light system.

data TrafficLight = Red | Yellow | Green deriving (Show, Eq)

Step 2: Define the Transition Function


Next, we'll create a function to handle the transition between the different traffic light states.

transition :: TrafficLight -> TrafficLight
transition Red = Green
transition Yellow = Red
transition Green = Yellow

Step 3: Simulate the Traffic Light


We'll write a function to simulate the traffic light over a series of transitions.

simulateTrafficLight :: TrafficLight -> Int -> IO ()
simulateTrafficLight \_ 0 = return ()
simulateTrafficLight state cycles = do
print state
let newState = transition state
simulateTrafficLight newState (cycles - 1)

Step 4: Testing


We can write a simple test function to verify our transitions.

testTransitions :: Bool
testTransitions =
transition Red == Green &&
transition Green == Yellow &&
transition Yellow == Red

Step 5: Running the Simulation


We'll write a main function to run our simulation.

main :: IO ()
main = do
let cycles = 10
putStrLn $ "Running traffic light simulation for " ++ show cycles ++ " cycles:"
simulateTrafficLight Red cycles
putStrLn "Simulation complete."
putStrLn $ "Transition tests passed: " ++ show testTransitions

Step 6: Compiling and Running


To compile and run the code, save it in a file called TrafficLight.hs and run the following commands:

ghc -o TrafficLight TrafficLight.hs
./TrafficLight

You should see the following output:

Running traffic light simulation for 10 cycles:
Red
Green
Yellow
Red
Green
Yellow
Red
Green
Yellow
Red
Simulation complete.
Transition tests passed: True

Wrapping Up


You now have a simple traffic light system simulated in Haskell! You can experiment with different starting states and numbers of cycles to further explore the behavior of the system.

Feel free to extend the code with additional features, such as timing for each state, more complex transition logic, or incorporating pedestrian signals.

Happy Haskell Programming!

Supporting My Work


Please consider Buying Me A Coffee. I work hard to bring you my best content and any support would be greatly appreciated. Thank you for your support!


Eric David Smith
Father / Software Engineer / Musician / Entrepreneur

Discover More (3) Tutorials


Blog Tags