Skip to content

Commit

Permalink
Update stub and example
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Jan 9, 2024
1 parent f09e8f3 commit f399a27
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
44 changes: 44 additions & 0 deletions exercises/practice/robot-simulator/.meta/example.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use context essentials2020

provide-types *

include string-dict

data Axis: axis(label, left, right) end

cardinal_axes = [string-dict:
'north', axis('north', 'west', 'east'),
'south', axis('south', 'east', 'west'),
'east', axis('east', 'north','south'),
'west', axis('west','south', 'north'),
]

data Robot:
| robot(x :: NumInteger, y :: NumInteger, direction :: String) with:
method move(self, directions :: String) -> Robot:
foldl(
lam(acc, elt):
direction =
ask:
| elt == 'L' then: cardinal_axes.get-value(self.direction).left
| elt == 'R' then: cardinal_axes.get-value(self.direction).right
| otherwise: self.direction
end
{x; y} =
if elt == 'A':
ask:
| direction == 'north' then: {acc.x; acc.y + 1}
| direction == 'south' then: {acc.x; acc.y - 1}
| direction == 'east' then: {acc.x + 1; acc.y}
| direction == 'west' then: {acc.x - 1; acc.y}
end
else:
{acc.x; acc.y}
end

robot(x, y, direction)
end,
self,
string-explode(directions))
end
end
37 changes: 2 additions & 35 deletions exercises/practice/robot-simulator/robot-simulator.arr
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,8 @@ use context essentials2020

provide-types *


CARDINAL_DIRECTIONS = [list:
'north',
'east',
'south',
'west'
]


data Robot:
| robot(x :: NumInteger, y :: NumInteger, direction :: String) with:
method move(self, directions :: String) -> Robot:
foldl(
lam(acc, elt):
cases (String) elt:
| 'L' =>
new-direction = ''
robot(acc.x, acc.y, new-direction)
| 'R' =>
new-direction = ''
robot(acc.x, acc.y, new-direction)
| 'A' =>
cases (String) acc.direction:
| 'north'
robot(0, acc.y + 1, self.direction)
| 'east'
robot(acc.x + 1, 0, self.direction)
| 'south'
robot(0, acc.y - 1, self.direction)
| 'west'
robot(acc.x - 1, 0, self.direction)
end
end
end,
self,
string-explode(directions))
| robot(x :: NumInteger, y :: NumInteger, direction :: String)
...
end
end

0 comments on commit f399a27

Please sign in to comment.