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

chauchakching's solution

to Robot Name in the Haskell Track

Published at May 30 2020 · 0 comments
Test suite


This exercise has changed since this solution was written.

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. http://gschool.it

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'
module Robot
  ( Robot
  , initialState
  , mkRobot
  , resetName
  , robotName

import           Control.Monad.State            ( StateT
                                                , get
                                                , put
import qualified Data.Map.Strict               as M
import qualified System.Random                 as R
import qualified Data.UUID as U
import qualified Data.UUID.V4                  as U
import           Control.Monad.IO.Class         ( liftIO )
import           Control.Concurrent.STM.TMVar
import           Control.Monad.STM

data Robot = Robot {
                    robotId :: RobotId
                  , name :: TMVar String

type RobotId = String
type RunState = M.Map RobotId (TMVar String)

initialState :: RunState
initialState = M.empty

mkRobot :: StateT RunState IO Robot
mkRobot = do
  robotMap <- get
  newId    <- liftIO $ U.toString <$> U.nextRandom
  uniqueName <- liftIO $ uniqueNewName robotMap
  newName  <- liftIO $ newTMVarIO uniqueName
  put $ M.insert newId newName robotMap
  return $ Robot newId newName

resetName :: Robot -> StateT RunState IO ()
resetName robot = do
  robotMap <- get
  newName  <- liftIO $ uniqueNewName robotMap
  _ <- liftIO $ atomically $ swapTMVar (name robot) newName
  return ()

uniqueNewName :: RunState -> IO String
uniqueNewName robotMap = do
  tmpName <- liftIO randomRobotName
  names <- atomically $ mapM readTMVar $ M.elems robotMap
  if elem tmpName names then uniqueNewName robotMap else return tmpName

robotName :: Robot -> IO String
robotName robot = atomically $ readTMVar $ name robot

randomRobotName :: IO String
randomRobotName = sequence $ (replicate 2 randAlpha) ++ (replicate 3 randDigit)
  randAlpha = R.randomRIO ('A', 'Z')
  randDigit = R.randomRIO ('0', '9')

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?