🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of minosiants

minosiants's solution

to Robot Name in the Haskell Track

Published at Mar 19 2021 · 0 comments
Test suite

Manage robot factory settings.

When a robot comes off the factory floor, it has no name.

The first time you turn on a robot, a random name is generated in the format of two uppercase letters followed by three digits, such as RX837 or BC811.

Every once in a while we need to reset a robot to its factory settings, which means that its name gets wiped. The next time you ask, that robot will respond with a new random name.

The names must be random: they should not follow a predictable sequence. Using random names means a risk of collisions. Your solution must ensure that every existing robot has a unique name.


To complete this exercise, you need to create the data type Robot, as a mutable variable, and the data type RunState. You also need to implement the following functions:

  • initialState
  • mkRobot
  • resetName
  • robotName

You will find a dummy data declaration and type signatures already in place, but it is up to you to define the functions and create a meaningful data type, newtype or type synonym. To model state this exercise uses the State monad. More specifically we combine the State monad with the IO monad using the StateT monad transfomers. All tests are run with initialState as the state fed into to evalStateT.

Getting Started

Please refer to the installation and learning help pages.

Running the tests

To run the test suite, execute the following command:

stack test

If you get an error message like this...

No .cabal file found in directory

You are probably running an old stack version and need to upgrade it.

Otherwise, if you get an error message like this...

No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...

Just do as it says and it will download and install the correct compiler version:

stack setup

Running GHCi

If you want to play with your solution in GHCi, just run the command:

stack ghci

Feedback, Issues, Pull Requests

The exercism/haskell repository on GitHub is the home for all of the Haskell exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!


A debugging session with Paul Blackwell at gSchool.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


name: robot-name

  - base
  - mtl

  exposed-modules: Robot
  source-dirs: src
    - random

    main: Tests.hs
    source-dirs: test
      - robot-name
      - hspec


module Robot (Robot, initialState, mkRobot, resetName, robotName) where

import           Control.Concurrent.MVar (MVar, newMVar, readMVar, swapMVar)
import           Control.Monad           (void)
import           Control.Monad.State     (StateT)
import           Control.Monad.Trans     (lift)
import           System.Random           (randomRIO)

newtype Robot = Robot { robotNameVar :: MVar String }
type RunState = ()

initialState :: RunState
initialState = ()

randomName :: IO String
randomName = mapM randomRIO [letter, letter, digit, digit, digit]
    letter = ('A', 'Z')
    digit = ('0', '9')

mkRobot :: StateT RunState IO Robot
mkRobot = Robot <$> lift (randomName >>= newMVar)

resetName :: Robot -> StateT RunState IO ()
resetName (Robot name) = void . lift $ randomName >>= swapMVar name

robotName :: Robot -> IO String
robotName = readMVar . robotNameVar


{-# OPTIONS_GHC -fno-warn-type-defaults #-}

import Control.Monad       (replicateM, unless)
import Control.Monad.State (evalStateT)
import Control.Monad.Trans (lift)
import Data.Ix             (inRange)
import Data.List           (group, intercalate, sort)
import Test.Hspec          (Spec, expectationFailure, it, shouldBe, shouldNotBe, shouldSatisfy)
import Test.Hspec.Runner   (configFastFail, defaultConfig, hspecWith)

import Robot (initialState, mkRobot, resetName, robotName)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = do

          let a = ('A', 'Z')
          let d = ('0', '9')
          let matchesPattern s = length s == 5
                                 && and (zipWith inRange [a, a, d, d, d] s)
          let testPersistence r = do
                n1 <- robotName r
                n2 <- robotName r
                n3 <- robotName r
                n1 `shouldBe` n2
                n1 `shouldBe` n3
          let evalWithInitial = flip evalStateT initialState

          it "name should match expected pattern" $
            evalWithInitial mkRobot >>= robotName >>= (`shouldSatisfy` matchesPattern)

          it "name is persistent" $
            evalWithInitial mkRobot >>= testPersistence

          it "different robots have different names" $
            evalWithInitial $ do
              robots <- replicateM 5000 mkRobot
              names <- traverse (lift . robotName) robots
              let repeats = map head . filter ((>1) . length) . group . sort $ names
              lift $ unless (null repeats) $
                expectationFailure $ "Repeat name(s) found: " ++ intercalate ", " repeats

          it "new name should match expected pattern" $
            evalWithInitial $ do
              r <- mkRobot
              resetName r
              lift $ robotName r >>= (`shouldSatisfy` matchesPattern)

          it "new name is persistent" $
            evalWithInitial $ do
              r <- mkRobot
              resetName r >> lift (testPersistence r)

          it "new name is different from old name" $
            evalWithInitial $ do
              r <- mkRobot
              n1 <- lift $ robotName r
              resetName r
              n2 <- lift $ robotName r
              lift $ n1 `shouldNotBe` n2

          it "resetting a robot affects only one robot" $
            evalWithInitial $ do
              r1 <- mkRobot
              r2 <- mkRobot
              n1 <- lift $ robotName r1
              n2 <- lift $ robotName r2
              lift $ n1 `shouldNotBe` n2
              resetName r1
              n1' <- lift $ robotName r1
              n2' <- lift $ robotName r2
              lift $ n1' `shouldNotBe` n2'
              lift $ n2  `shouldBe`    n2'

-- 9ac11efd2c9fbdd190ecd7cd1a53904052ee0e65
module Robot (Robot, initialState, mkRobot, resetName, robotName) where

import Control.Monad (replicateM)
import Control.Monad.State
import Control.Monad.Trans (lift)
import System.Random (Random (randomRIO))
import qualified Data.Set as S 
import Data.Set
import Data.IORef 
type RunStateIO = StateT RunState IO

type Name = String

newtype Robot = Robot {name ::(IORef Name)} deriving (Eq)

newtype RunState = RunState {names :: Set Name} deriving (Show, Eq)

initialState :: RunState
initialState = RunState empty

mkRobot :: RunStateIO Robot
mkRobot = do 
  newName <- uniqueName
  noname <- lift $ newIORef newName
  return $ Robot noname 

resetName :: Robot -> RunStateIO ()
resetName robot = do
  names' <- gets names
  newName <- uniqueName
  oldName <- lift $ robotName robot   
  let res = delete  oldName  names'
  put (RunState (insert newName res))

robotName :: Robot -> IO String
robotName robot = readIORef $ name robot

uniqueName :: RunStateIO String
uniqueName = do
  newName <- lift nameGen
  n <- gets names 
  if newName `elem` n
    then uniqueName
    else return newName

nameGen :: IO String
nameGen = do
  num <- randomRIO (100 :: Int, 999)
  pref <- replicateM 2 (randomRIO ('A', 'Z'))
  return $ pref ++ show num

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?