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.98 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.97 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.96 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.95 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.94 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.93 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.92 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.91 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.90 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.89 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.88 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.87 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.86 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.85 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.84 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.83 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.82 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.81 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.80 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.79 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs