Robots You are given the interface (signature) of the module Robots. 1 module type Robots = 2 sig 3 type t 4 val create : bool array array -> ( int -> int * int -> int * int ) -> t 5 val move : t -> int * int -> unit 6 val print : t -> unit 7 end ;; Define the structure of the module Robots for handling a two-dimensional (2D) world of robots. The 2D world is composed of NxN cells. Each cell can contain 3 a robot. We have only one type of robots. A robot moves in the 2D world by using a function next_pos : int->int*int->int*int which, given an N and a current position of a robot, returns a next position within the borders of the 2D world. Define a type Robots.t that is used as the abstract data structure. It includes all data used for the representation of a robot world. Write the following three functions. • create : bool array array -> (int -> int * int -> int * int) -> Robots.t, where the first argument is a bool matrix indicating the initial positions of the robots, and the second argument is the function next_pos. • move : Robots.t->(int*int)->unit, which makes the next legal move of a robot at the position given as the second argument. If a robot can not move (i.e. destination is already occupied/there is no robot at the starting position) to a location provided by next_pos, then the robot stays where it is. • print : Robots.t->unit that prints the robot world on screen. ”R” should indicate a robot on that position and ”x” should indicate that there is no robot at that position. A sample function next_pos is provided. The function returns the adjacent cell in the following pattern: → → → → ↓ ↑ → → ↓ ↓ ↑ ↑ · ↓ ↓ ↑ ↑ ← ← ↓ ↑ ← ← ← ← 2 # let next_pos n (x , y ) = match min (n -x -1) (n -y -1) | > min x | > min y with 3 | z when x = z && y <( n -z -1) -> (x , y +1) 4 | z when y = z && x >z - > (x -1 , y ) 5 | z when z =n -x -1 && y >z - > (x ,y -1) 6 | z when z =n -y -1 && x <( n -z -1) -> ( x +1 , y ) 7 | _ -> (x , y ) ;; 8 # let a = Robots . create ([|[| false ; true ; false |]; [| false ; false ; false |];[| true ; false ; false |]|]) next_pos ;; 9 # Robots . print a ;; 10 xRx 11 xxx 12 Rxx 13 - : unit = () 4 14 # Robots . move a (1 ,0) ;; 15 - : unit = () 16 # Robots . print a ;; 17 xRx 18 xxx 19 Rxx 20 - : unit = () 21 # Robots . move a (0 ,1) ;; 22 - : unit = () 23 # Robots . print a ;; 24 xxR 25 xxx 26 Rxx 27 - : unit = () 28 # Robots . move a (0 ,2) ;; 29 - : unit = () 30 # Robots . print a ;; 31 xxx 32 xxR 33 Rxx 34 - : unit = () 35 # Robots . move a (1 ,2) ;; 36 - : unit = () 37 # Robots . print a ;; 38 xxx 39 xxx 40 RxR 41 - : unit = () 42 # Robots . move a (2 ,2) ;; 43 - : unit = () 44 # Robots . print a ;; 45 xxx 46 xxx 47 RRx 48 - : unit = () 49 # Robots . move a (2 ,1) ;; 50 - : unit = () 51 # Robots . print a ;; 52 xxx 53 xxx 54 RRx 55 - : unit = ()
Robots
You are given the interface (signature) of the module Robots.
1 module type Robots =
2 sig
3 type t
4 val create : bool array array -> ( int -> int * int
-> int * int ) -> t
5 val move : t -> int * int -> unit
6 val print : t -> unit
7 end ;;
Define the structure of the module Robots for handling a two-dimensional (2D)
world of robots. The 2D world is composed of NxN cells. Each cell can contain
3
a robot. We have only one type of robots. A robot moves in the 2D world by
using a function next_pos : int->int*int->int*int which, given an N and
a current position of a robot, returns a next position within the borders of the
2D world. Define a type Robots.t that is used as the abstract data structure.
It includes all data used for the representation of a robot world.
Write the following three functions.
• create : bool array array -> (int -> int * int -> int * int)
-> Robots.t, where the first argument is a bool matrix indicating the
initial positions of the robots, and the second argument is the function
next_pos.
• move : Robots.t->(int*int)->unit, which makes the next legal move
of a robot at the position given as the second argument. If a robot can not
move (i.e. destination is already occupied/there is no robot at the starting
position) to a location provided by next_pos, then the robot stays where
it is.
• print : Robots.t->unit that prints the robot world on screen. ”R”
should indicate a robot on that position and ”x” should indicate that
there is no robot at that position.
A sample function next_pos is provided. The function returns the adjacent
cell in the following pattern:
→ → → → ↓
↑ → → ↓ ↓
↑ ↑ · ↓ ↓
↑ ↑ ← ← ↓
↑ ← ← ← ←
2 # let next_pos n (x , y ) = match min (n -x -1) (n -y -1) | >
min x | > min y with
3 | z when x = z && y <( n -z -1) -> (x , y +1)
4 | z when y = z && x >z - > (x -1 , y )
5 | z when z =n -x -1 && y >z - > (x ,y -1)
6 | z when z =n -y -1 && x <( n -z -1) -> ( x +1 , y )
7 | _ -> (x , y ) ;;
8 # let a = Robots . create ([|[| false ; true ; false |]; [|
false ; false ; false |];[| true ; false ; false |]|])
next_pos ;;
9 # Robots . print a ;;
10 xRx
11 xxx
12 Rxx
13 - : unit = ()
4
14 # Robots . move a (1 ,0) ;;
15 - : unit = ()
16 # Robots . print a ;;
17 xRx
18 xxx
19 Rxx
20 - : unit = ()
21 # Robots . move a (0 ,1) ;;
22 - : unit = ()
23 # Robots . print a ;;
24 xxR
25 xxx
26 Rxx
27 - : unit = ()
28 # Robots . move a (0 ,2) ;;
29 - : unit = ()
30 # Robots . print a ;;
31 xxx
32 xxR
33 Rxx
34 - : unit = ()
35 # Robots . move a (1 ,2) ;;
36 - : unit = ()
37 # Robots . print a ;;
38 xxx
39 xxx
40 RxR
41 - : unit = ()
42 # Robots . move a (2 ,2) ;;
43 - : unit = ()
44 # Robots . print a ;;
45 xxx
46 xxx
47 RRx
48 - : unit = ()
49 # Robots . move a (2 ,1) ;;
50 - : unit = ()
51 # Robots . print a ;;
52 xxx
53 xxx
54 RRx
55 - : unit = ()
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)