(0,0)
of an
infinite grid. The basic commands of the robot control language are
simple moves which intruct the robot to move North, East,
South
or West
. This is represent in Ocaml as:
type move = N | E | S | WIf a robot is at position
(x,y)
then a command
N
moves it to position (x+1,y)
, a command
E
moves it to position (x,y+1)
, and so on.
interp
of type move -> (int,int)
-> (int,int)
. The function should take a move, the current
position of the robot, and return the new position. For example,
interp N (4,2)should return
(5,2)
.
type move = N | E | S | W | Sequence of move listExtend the function
interp
to implement this new
case. For example,
interp (Sequence [N; N; N; N]) (5,2)
returns
(9,2)
interp (Sequence [N; N; Sequence [E; E; E]; N; N])
(5,2)
returns (9,5)
interp (Sequence [N; Sequence [E; E; E]; N; Sequence [E; E;
E]; N; Sequence [E; E; E]; N]) (5,2)
returns (9,11)
.
interp (Define("3E", Sequence [E;E;E], Sequence [N; Invoke "3E"; N; Invoke "3E"; N; Invoke "3E"; N])) (5,2)In other words the command language is now:
type move = N | E | S | W | Sequence of move list | Define of string * move * move | Invoke of stringModify and extend the function
interp
appropriately.