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

platoali's solution

to Robot Name in the Haskell Track

Published at May 03 2021 · 0 comments
Instructions
Test suite
Solution

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.

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

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!

Source

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.

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, 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.State(StateT,get,lift,modify)
import System.Random (randomRIO)
import Data.IORef (IORef,newIORef,writeIORef,readIORef)
import Control.Monad.Loops (untilJust)
import Data.List (delete) 

type  Robot = IORef String
type  RunState = [String]

initialState :: RunState
initialState = []

mkRobot :: StateT RunState IO Robot
mkRobot =   do
  s <- get
  newName <- lift $  untilJust $ notUsed s generateName 
  modify (newName:)
  lift $ newIORef newName
  


resetName :: Robot -> StateT RunState IO ()
resetName robot =  do
  s <- get
  oldName <-  lift $ robotName robot
  newName <- lift $ untilJust $  notUsed s  generateName
  modify (delete oldName)
  modify (newName:)
  lift $ writeIORef  robot newName  

robotName :: Robot -> IO String
robotName  = readIORef 


generateName ::  IO  String
generateName =  let
  genChar =  randomRIO ('A','Z')
  genNum =   randomRIO ('0','9')
  in do
  c1 <- genChar
  c2 <- genChar
  n1 <- genNum
  n2 <- genNum 
  n3 <- genNum
  return  [c1,c2,n1,n2,n3]

notUsed :: RunState -> IO String -> IO (Maybe String) 
notUsed s  name  = fmap (\str -> if elem str s then Nothing else Just str) name

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?