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 shouldnot cut-and-paste.
In this code-along, we will explore how to use the higher-function map to translate an integer list into a variety of vertex lists. We then show how these vertex lists can be concatenated and how map can again be used to connect the vertices in the resulting list to a central vertex.
The Bricklayer program in this example creates the graph shown below. It contains 16*4 + 1 = 65 vertices and 16*4 = 64 edges. In this graph, 1 central vertex c is connected via blue edges to 64 peripheral vertices, p0, …, p63.
In this program, the central vertex is statically defined (line 10) in terms of the variable half. The peripheral vertices are created dynmically (i.e., during runtime) in the following manner. First, a listvs is statically defined (line 12) consisting of consecutive integers in the range 0…15. Then 4 user-defined functions are declared which translate integers into 2D coordinates. In a manner similar to that discussed in the previous code-along, the coordinates produced by these functions make use of variables. In this case, the variables delta and max are used.
The following things about how vertex lists are created are noteworthy.
- When passed to the higher-order function map (lines 14-17), each function creates list of 16 vertices.
- When seen as a whole, the four function calls to map create 64 vertices which are evenly distributed and fall on the 4 sides of a square-shaped region in the virtual space.
- The vertex lists created are disjoint (i.e., they have no vertices in common).
- Vertices are created that lie exactly on the 4 corners of the virtual square.
- Each of the four lists created corresponds to one side of a virtual square.
- Each of the four lists created contains one corner vertex.
- Each of the four lists created contains one vertex which lies on the midpoint of a side.
The user-defined function multiconnect is used to connect the peripheral vertices to the central vertex. The behavior of this function is the same here as it was in the previous code-along. In the call, the third argument to multiconnect (line 26) is a list that consists of the the concatenation of the 4 lists of peripheral vertices.
The Bricklayer program in this example creates the graph-like artifact shown below.
The algorithm used to create this artifact is similar to that used in the previous example. The only difference is that one additional function call (line 27) is added that overwrites the graph with a ring of empty bricks. It is this ring that produces the effect shown.