Sha256: 489a2ae09649b08c247f7a3f8ca0a713c4fa661a2fd549ceed96599d037c0c61

Contents?: true

Size: 1.59 KB

Versions: 396

Compression:

Stored size: 1.59 KB

Contents

module Robot (Bearing(..),
              mkRobot,
              coordinates,
              bearing,
              simulate,
              turnRight,
              turnLeft
              ) where
import Data.List (foldl')
import Control.Arrow (first, second)

data Movement = TurnLeft
              | TurnRight
              | Advance
              deriving (Show, Eq, Enum)

data Bearing = North
               | East
               | South
               | West
               deriving (Show, Eq, Enum, Bounded)

type Coordinates = (Int, Int)

data Robot = Robot { bearing :: Bearing
                   , coordinates :: Coordinates
                   } deriving (Show, Eq)
mkRobot :: Bearing -> Coordinates -> Robot
mkRobot = Robot

advance :: Bearing -> Coordinates -> Coordinates
advance dir = case dir of
  North -> second succ
  East  -> first succ
  South -> second pred
  West  -> first pred

turnLeft, turnRight :: Bearing -> Bearing
turnLeft d | d == minBound = maxBound
           | otherwise     = pred d

turnRight d | d == maxBound = minBound
            | otherwise     = succ d

instructions :: String -> [Movement]
instructions = map fromChar
  where fromChar c = case c of
          'L' -> TurnLeft
          'R' -> TurnRight
          'A' -> Advance
          _   -> error ("Unknown movement " ++ show c)

step :: Movement -> Robot -> Robot
step Advance   r = r { coordinates = advance (bearing r) (coordinates r) }
step TurnLeft  r = r { bearing = turnLeft (bearing r) }
step TurnRight r = r { bearing = turnRight (bearing r) }

simulate :: Robot -> String -> Robot
simulate r = foldl' (flip step) r . instructions

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.179 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.178 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.177 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.176 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.175 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.174 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.173 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.172 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.171 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.170 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.169 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.167 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.166 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.165 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.164 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.163 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.162 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.161 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.160 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs