A code-along is a story about coding in which you are encouraged to “code up” all the examples in the story. In a code-along you should not cut-and-paste.
In this code-along we will take a look two different approaches for creating an 8×8 grid of crosses.
Program 1 – An Arithmetic Algorithm
The program below contains three function declarations: cross1, row, and rows. When called, the function cross1 will create a cross at a given location whose width and height are three 1-bit bricks. When called, the function row creates a row of eight crosses. It accomplishes this in a very direct manner by simply calling cross1 eight times, each with a different column (i.e., x-value) location. When called, function rows constructs an 8×8 grid of crosses. It accomplishes this by leveraging the computation defined in the function row. Specifically, rows constructs a grid by called the row function eight times, each with a different row (i.e., y-value) location.
Note that the bodies of the user-defined functions collectively contain 4+8+8 = 20 function calls. Also note, that extending this approach to construct a 16×16 grid will increase the function calls in each of the bodies of row and rows by 8. Thus, the collective number of function calls in the bodies of the user-defined functions will total 4+16+16 = 36 when constructing a 16×16 grid.
Note that the coordinate shifting in both the row and rows functions follows an arithmetic progression. For this reason, we refer to this algorithm for creating a grid as an arithmetic algorithm.
open Level_2;
fun cross1 (x,z) =
(
put2D_2x1_BLUE(x ,z + 1);
put2D_1x1_BLUE(x + 2,z + 1);
put2D_1x1_BLUE(x + 1,z );
put2D_1x1_BLUE(x + 1,z + 2)
);
fun row (x,z) =
(
cross1(x + 0*4,z);
cross1(x + 1*4,z);
cross1(x + 2*4,z);
cross1(x + 3*4,z);
cross1(x + 4*4,z);
cross1(x + 5*4,z);
cross1(x + 6*4,z);
cross1(x + 7*4,z)
);
fun rows (x,z) =
(
row(x,z + 0*4);
row(x,z + 1*4);
row(x,z + 2*4);
row(x,z + 3*4);
row(x,z + 4*4);
row(x,z + 5*4);
row(x,z + 6*4);
row(x,z + 7*4)
);
build2D(64,64);
rows (0,0);
show2D "grid";
Program 2 – A Geometric Algorithm
The program below contains four function declarations: cross1, cross4, cross16, and cross64. When called, the function cross1 will create a cross at a given location whose width and height are three 1-bit bricks.
Note that the bodies of the user-defined functions collectively contain 4+4+4+4 = 16 function calls. Also note, that extending this approach to construct a 16×16 grid will require the declaration of an additional function, called cross256. The function cross256 is similar to the declarations cross4, cross16, and cross64. In particular, it contains four calls to the function cross16. In this approach, the collective number of function calls in the bodies of the user-defined functions will total 4+4+4+4+4 = 20 when constructing a 16×16 grid.
Note that the coordinate shifting in the cross4, cross16, and cross64 functions follows a geometric progression. For this reason, we refer to this algorithm for creating a grid as a geometric algorithm.
open Level_2;
fun cross1 (x,z) =
(
put2D_2x1_BLUE(x ,z + 1);
put2D_1x1_BLUE(x + 2,z + 1);
put2D_1x1_BLUE(x + 1,z );
put2D_1x1_BLUE(x + 1,z + 2)
);
fun cross4 (x,z) =
(
cross1(x ,z );
cross1(x + 2*2,z );
cross1(x + 2*2,z + 2*2);
cross1(x ,z + 2*2)
);
fun cross16 (x,z) =
(
cross4(x ,z );
cross4(x + 2*2*2,z );
cross4(x + 2*2*2,z + 2*2*2);
cross4(x ,z + 2*2*2)
);
fun cross64 (x,z) =
(
cross16(x ,z );
cross16(x + 2*2*2*2,z );
cross16(x + 2*2*2*2,z + 2*2*2*2);
cross16(x ,z + 2*2*2*2)
);
build2D(64,64);
cross64 (0,0);
show2D "grid";