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, you will learn the geometric algorithm for constructing a basic lace pattern which involves an initial seed shape and a stamping pattern. The seed shape we will use is a bit-brick (i.e., a 1×1 brick), and the stamping pattern we will use resembles the reverse of the letter L.
This code-along will also use a Bricklayer function, called generateRandomBrickFn, to generate random bricks within our lace artifact. This will enable us to add color to lace artifacts in interesting ways.
In this step we will write a program that creates a lace using (1) a 1×1 brick as its seed shape, and (2) one iteration of the stamping pattern that is shown in the table below.
In the code below, information about the seed shape is stored in the variables seedShapeSide and theDimensions. This allows us to change the size of the seed shape from a 1×1 brick to an nxn brick by simply chaning the value to which the variable seedShapeSide is bound. Also note, that the variable theDimensions will be bound (at run-time) to a value that is a tuple consisting of two integer values.
The seedShape function is a function that when called creates a square of size theDimensions whose bricks are of the kind b positioned at the coordinate p. Note that it is perfectly acceptable to use a single variable p as a formal parameter to denote a coordinate.
The function lace0 implements a lace pattern by placing seed shapes at the locations specified by the reverse-L stamping pattern in the table above. The body of lace0 is a let-block in which a val-declaration is used to locally declare the variable delta, which will be bound to the value of the seedShapeSide at run-time. In lace0, delta and seedShapeSide are equivalent, so there is no compelling reason for introducing delta. However, as we interate the stamping pattern to create larger and larger reverse-L lace artifacts, the value of delta and seedShapeSide will not be equivalent. More specifically, the value of delta will be a power of 2 times the value of the seedShapeSide.
This example creates a reverse-L lace using 6 iterations of the stamping pattern. Notice that this example makes use of a function, called pow, to compute integer powers of 2. We have not covered how to write these kinds of functions so it is not necessary to understand the inner workings of such function declarations. At this time, the only thing you need to understand about the pow function is (1) what it does, and (2) how to call it. Let v and n denote two integers. The function call pow v n will compute vn.
In the code below, the bodies of the lace0 – lace5 functions differ from one another in only two places: (1) the second argument given to the function pow, and (2) the function that is called in the body of the let-block.
This step explores a way to introduce color into the lace artifact by associating a hard-coded brick value with a given lace function call. In this example, hard-coding will involve replacing instances of the variable brick with actual brick values (e.g., BLUE).
Note that all of the lace function declarations have brick as a formal parameter. It is through these parameters that brick colors “flow” from a lace call all the way down to the seedShape function calls which create the lace. From this perspective, the variable brick, which is a formal parameter of all the lace functions creates a kind of plumming that transports values introduced at a source location (e.g., the initial call to the function lace5) with a destination location (i.e., the seedShape function) where these values are actually used.
With this metaphor in mind, consider the following function call:
lace3 YELLOW (0,0);
The lace artifact created by this function call is the composition of three laces created by calls to the function lace2. In turn, the lace artifact created by a function call to lace2 is the composition of three laces created by calls to the function lace1, and so on.
When constructing a lace artifact using a lace3 function call, consider what would happen if each of the three calls to lace2 was made using a different hard-coded brick value. For example, suppose the first call to lace2 used RED, the second call to lace2 used GREEN, and the third call to lace2 used BLUE. In this case, the lace artifact created using the lace3 function would be the composition of a RED, a GREEN, and a BLUE lace each of which created by corresponding calls to the function lace2.
Note that in this case, the actual parameter YELLOW, which is used in the lace3 function call is completely disregarded. One way to understand what is happening here is that the flow of brick values coming into the function lace3 is disregarded and a new hard-coded flow of brick values is introduced. It is also worth mentioning that the choice of interrupting the flow in the lace3 function is completely arbitrary. The way the code is written, it is easy to introduce hard-coded values at any level of the artifact construction.
This version of the code replaces the hard-coded values that were introduced in the body of the lace3 function in the previous example with randomly generated values. Specifically, Bricklayer’s generateRandomBrickFn is used to create the nullary function randomBrickFn that, when called, generates a brick randomly selected from the brick list called blueScale, which is provided by Bricklayer. Note that Bricklayer provides a number of such predefined brick lists which can be used in place of the blueScale list used in this example. An incomplete set of lists is shown below.
- blueScale (used in this example)
Note that the execution of a lace3 function call will result in 3 distinct calls to the lace2 function. Similarly, the execution of a lace4 function call will result in 3×3 = 9 distinct calls to the lace2 function. The code is written in such a way that each time the lace2 function is called it is given a randomly generated brick.
There are many points in the program flow where randomly generated bricks can be introduced. Also, more than one random generator can be used to generate bricks. For example, one random generator can be used to select from the blueScale bricks and another can be used to select from the redScale bricks.
This version of the code slightly improves the previous code by moving the location where the new (i.e., randomly selected) brick is introduced into the code flow. In the previous implementation, 3 randomly selected bricks were introduced in the body of the lace3 function. Each of these bricks was then passed to the function lace2. Computationally, this is equivalent to introducing a single randomly selected brick in the body of the lace2 function. In other words, rather than introducing a randomly selected brick at the “point of call” to the function lace2, we introduce a randomly selected brick at the “point of entry” to the function body of lace2.
This example involves restructuring the code so that all lace sizes are created using an applyLace function. The applyLace function takes as input a stamp function, a brick, a shift value delta, and a coordinate. Its purpose is to perform the reverse-L stamping pattern and nothing more. The lace0 – lace5 functions are defined in terms of applyLace. For example, the lace0 function directs the applyLace function to perform its stamping pattern using seedShape as its stamp. Similarly, the lace1 function directs the applyLace function to perform its stamping pattern using lace0 as its stamp, and so on.
Step 6 – Some final polish.
This last example involves some minor code polishing. Specifically, a function is introduced, called computeDelta, whose purpose is to compute the appropriate delta for a given power of 2.