Two Beads, Four Colour Choices
Peyote stitch has a satisfying rhythm: pick up a bead, pass the needle through a bead from the previous row, and pull until the new bead settles into the gap. Choosing the next colour can take longer than making the stitch.
Suppose I have two colours on the mat, blue and gold, and want to audition every ordered pair for a motif. There are four: blue–blue, blue–gold, gold–blue, and gold–gold. The order matters because the first colour goes in the first position. This is a Cartesian product: pair every item in one collection with every item in another.
Scheme makes the two choices visible as nested calls to for-each:
(import (scheme base) (scheme write))
(define colours '(blue gold))
(for-each
(lambda (first)
(for-each
(lambda (second)
(write (list first second))
(newline))
colours))
colours)
Java expresses the same idea with a loop inside a loop. Save this as Beads.java and run it with java Beads.java:
class Beads {
public static void main(String[] args) {
String[] colours = {"blue", "gold"};
for (String first : colours) {
for (String second : colours) {
System.out.println(first + " " + second);
}
}
}
}
Both programs print four pairs in the same order. Neither knows how to stitch them: the pairs are colour candidates, not a peyote pattern. I still need to decide where they fit, keep the thread tension even, and secure the thread when it runs short.
Cartesian products became a practical part of computing through relational databases in the 1970s. They are useful for exploring possibilities, but the number of results grows quickly: two collections of 100 items produce 10,000 pairs. On a bead mat, that is a lot of auditioning; in a database, it is a good reason to ask which pairs you actually need.