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

joe-warren-roo's solution

to Robot Name in the Haskell Track

Published at Sep 25 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Manage robot factory settings.

When robots come off the factory floor, they have no name.

The first time you boot them up, 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 their name gets wiped. The next time you ask, it will respond with a new random name.

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

Hints

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

For installation and learning resources, refer to the exercism help page.

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!

Source

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.

package.yaml

name: robot-name

dependencies:
  - base
  - mtl

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

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

Robot.hs

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]
  where
    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

Tests.hs

{-# 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, null, 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) where

import Control.Monad.State (StateT, get, modify)
import Control.Monad.Random
import Data.IORef

-- just throwing this out there, the description for this was obtuse
-- I don't get the point behind this requiring IORefs and StateT

type Robot = IORef String
type RunState = [String]

initialState :: RunState
initialState = []

randomName :: IO String
randomName = sequence $ (replicate 2 $ randomRIO ('A', 'Z')) ++ (replicate 3 $ randomRIO('0', '9'))

mkRobot :: StateT RunState IO Robot
mkRobot = do 
    name <- lift randomName
    existing <- get
    if elem name existing 
        then mkRobot
        else do
                modify (name:)
                ref <- lift $ newIORef name
                return ref

resetName :: Robot -> StateT RunState IO ()
resetName ref = do
    name <- lift randomName
    existing <- get
    if elem name existing 
        then resetName ref
        else do
                modify (name:)
                lift $ writeIORef ref name
                return ()

robotName :: Robot -> IO String
robotName = readIORef

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?