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.119 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.118 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.117 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.116 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.115 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.114 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.113 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.111 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.110 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.109 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.108 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.107 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.106 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.105 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.104 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.103 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.102 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.101 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.100 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs
trackler-2.2.1.99 tracks/haskell/exercises/robot-simulator/examples/success-standard/src/Robot.hs