-
-
Save Lysxia/d64cb2f57d20732557fb7d8e1f2dce5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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