Skip to content

Instantly share code, notes, and snippets.

@Lysxia
Last active November 5, 2019 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lysxia/d64cb2f57d20732557fb7d8e1f2dce5e to your computer and use it in GitHub Desktop.
Save Lysxia/d64cb2f57d20732557fb7d8e1f2dce5e to your computer and use it in GitHub Desktop.
data Robot = Robot Bearing Coordinates deriving (Eq, Show)
data Bearing = North | South | East | West deriving (Eq, Show)
left :: Bearing -> Bearing
left North = West
left West = South
left South = East
left East = North
right :: Bearing -> Bearing
right = left . left . left
type Coordinates = (Integer, Integer)
advance :: Bearing -> Coordinates -> Coordinates
advance North (x, y) = (x, y+1)
advance South (x, y) = (x, y-1)
advance East (x, y) = (x+1, y)
advance West (x, y) = (x-1, y)
move :: Robot -> String -> Robot
move = foldl f
where
f (Robot b c) 'A' = Robot b (advance b c)
f (Robot b c) 'L' = Robot (left b) c
f (Robot b c) 'R' = Robot (right b) c
f _ _ = error "move: undefined"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment