Look closely at a dhaka topi. The cloth is full of small shapes, diamonds, zigzags and stepped lines, and every shape comes back again and again, in rows, across the whole cap. A weaver in Palpa or Terhathum makes that pattern one thread at a time, choosing at every crossing of two threads which one shows.
More than two hundred years ago, in France, a loom learned to make those choices by itself. It read them from a chain of punched cards, one card for each row of the cloth. Try it now: tap squares to make a design, then press Weave.
The design has 8 rows. The cloth below it has 24 rows, and a real shawl might have thousands. How many cards does the loom need for a shawl of 600 rows, every repeat the same?
Pick an answer to see why.
You need: block programs, from Grade 6 (Programming with Blocks), and variables, from Grade 7 (Variables and Choices); the Python parts need loops and lists, from Grade 8.
Try it
Use the loom above.
- a.Tap one square of the design. How many squares of the cloth change? Why that many?
- b.Press Blank and draw a small shape of your own. Then press Weave and watch which card is lit as each row is woven. In what order are the cards read?
- c.Press Slant: a single diagonal line. Then choose Every second turned over. What happens to the cloth, and to the number of cards?
- d.With Diamond, try both kinds of repeat. Why does turning it over change nothing you can see?
So a loom with a chain of cards does two things a computer does: it follows instructions written down outside the machine, and it repeats them, the way a loop does.
How it works
Threads that cross
Cloth is two sets of threads at right angles. The long threads stretched on the loom are the warp. The thread that goes across, from side to side, one row at a time, is the weft. At every crossing, one of the two is on top. That is a choice with two answers, like a bit: warp on top, or weft on top.
To weave one row, the weaver lifts some of the warp threads. The weft is passed through the gap, under the lifted threads and over the rest. Then the lifted threads are let down, a different set is lifted, and the next row goes in.
In the loom on this page, the warp threads are red and the weft is cream. So a red square is a crossing where that warp thread was lifted. (In a dhaka topi it is the other way round: the colored threads are extra weft threads, laid in by hand. The idea is the same: a choice at every crossing.)
A card for each row
Before Jacquard, a large patterned loom needed a second person, a helper called a drawboy, to pull up the right threads for each row, following the pattern. Jacquard’s machine, made in France in 1804–05, put the pattern on punched cards instead.
Each warp thread hangs from a hook, and each hook has a needle through it. Above the hooks is the griffe, a set of bars that rises once for every row. For each row, a card is pressed against the needles’ ends. Where the card has a hole, the needle goes into it, so its hook stays over the griffe and is lifted, with its warp thread. Where there is no hole, the card pushes the needle back, the needle pushes its hook off the griffe, and that thread stays down. So the holes in one card say exactly which threads to lift for one row (Figure 31.2). A machine might have 100, 400 or 1,200 hooks.
The cards are laced edge to edge into an endless chain, and they go round and round through the machine. That is the loop in the widget: card 1, 2 … 8, and card 1 again.
A pattern repeats across the cloth too. In a Jacquard loom one hook can be tied to several warp threads, one in each repeat across, so a card with 8 holes can lift threads all the way across a wide cloth. That is why the loom on this page needs only 8 holes in a card for a cloth 48 threads wide.
A Jacquard card for one row has holes in places 2, 3, 6 and 7. What does the loom do?
Pick an answer to see why.
The pattern is not in the machine
Here is the big idea. To weave a different cloth, the weaver doesn’t rebuild the loom. They change the cards. The machine stays the same; only the instructions change. In a computer the same split has names: the machine is the hardware, and the instructions it follows are the software, the program.
Program it
With blocks: the turtle as the weft
The turtle can weave. Think of it as the weft thread going across the cloth. Where its pen is down, it draws a thick red line, one square long: a warp thread on top. Where its pen is up, it moves on without drawing: the weft shows. At the end of each row it lifts its pen, goes back to the left, one row up, and starts again.
Which squares are red? Here a rule decides, from the row number and the column number. The program starts with the rule for plain weave, the simplest cloth there is, and the ground cloth of dhaka: over one, under one, and the other way in the next row. In plain weave a thread is on top when the remainder of (col + row) ÷ 2 is 0.
Now change the rule to the remainder of (col + row) ÷ 4 < 2. Two red, two cream, and each row moved along by one: a diagonal pattern called twill. The jeans you wear are woven in twill, which is why their threads run in slanting lines.
In Python (from Grade 8): one card for each row
A design in Python is a list of strings, one string a row. '#' is a hole in the card, a thread lifted (red); '.' is no hole. This program punches the cards for the diamond: for each row it lists which threads to lift.
motif = [ '...##...', '..####..', '.##..##.', '##....##', '##....##', '.##..##.', '..####..', '...##...',] holes = 0for r in range(len(motif)): card = motif[r] lift = [] for c in range(len(card)): if card[c] == '#': lift.append(c + 1) holes = holes + 1 print('Card', r + 1, ': lift threads', lift)print(len(motif), 'cards,', holes, 'holes')Output
Card 1 : lift threads [4, 5]Card 2 : lift threads [3, 4, 5, 6]Card 3 : lift threads [2, 3, 6, 7]Card 4 : lift threads [1, 2, 7, 8]Card 5 : lift threads [1, 2, 7, 8]Card 6 : lift threads [2, 3, 6, 7]Card 7 : lift threads [3, 4, 5, 6]Card 8 : lift threads [4, 5]8 cards, 28 holesCard 3 is the card in Figure 31.2: threads 2, 3, 6 and 7.
Weave the cloth
Now the loom itself. For each row of the cloth, it takes the next card (motif[r % 8]: after card 8 comes card 1 again), and for each thread across, the place in the card that ties to it (card[c % 8]). Two loops, one inside the other, and the remainder does the repeating. The pixels module is this page’s own: show_grid draws the rows as squares, with a color for each character.
from pixels import show_grid motif = [ '...##...', '..####..', '.##..##.', '##....##', '##....##', '.##..##.', '..####..', '...##...',] def weave(motif, rows, cols): cloth = [] for r in range(rows): card = motif[r % len(motif)] line = '' for c in range(cols): line = line + card[c % len(card)] cloth.append(line) return cloth cloth = weave(motif, 24, 48)show_grid(cloth, {'#': '#b5302a', '.': '#f3ead2'})print(len(cloth), 'rows of', len(cloth[0]), 'squares:', len(cloth) * len(cloth[0]), 'squares in all')Output
24 rows of 48 squares: 1152 squares in allPicture
Change the motif and run it again. Try a design of your own, any size: 6 rows of 5, or 10 rows of 10. The % makes it repeat, whatever its size.
Weave by a rule
The turtle wove by a rule, with no cards at all. Python can too. This program weaves three cloths from three rules: plain weave, twill, and a diamond made from how far each square is from the middle of its repeat (abs() takes away the minus sign: abs(-3) is 3).
from pixels import show_grid def plain(r, c): return (r + c) % 2 == 0 def twill(r, c): return (r + c) % 4 < 2 def diamond(r, c): across = abs(c % 8 - 4) down = abs(r % 8 - 4) return across + down == 4 or across + down == 1 for rule in [plain, twill, diamond]: cloth = [] for r in range(16): line = '' for c in range(32): if rule(r, c): line = line + '#' else: line = line + '.' cloth.append(line) show_grid(cloth, {'#': '#b5302a', '.': '#f3ead2'}) print(rule.__name__, ': first row', cloth[0][:8])Output
plain : first row #.#.#.#.twill : first row ##..##..diamond : first row ....#...Picture
A function is passed to the loop as if it were a number: for rule in [plain, twill, diamond]. The loop doesn’t care what the rule is. Like the loom, it is the same machine for any pattern.
What your program shows
Your weave program turned 64 choices, the squares of an 8 × 8 design, into 1,152 squares of cloth. Make the cloth as big as a shawl and count again:
rows = 600threads = 400cards = 8holes = 8print('Crossings in the cloth:', rows * threads)print('Choices on the cards:', cards * holes)print('Each choice is woven', rows * threads // (cards * holes), 'times')Output
Crossings in the cloth: 240000Choices on the cards: 64Each choice is woven 3750 timesThat is what a loop is for. A pattern that repeats is a short program run again and again: 8 cards, read round and round, weave 240,000 crossings. And the pattern is outside the machine, on cards that can be changed, so the same loom weaves any design.
But a picture that never repeats can’t be shortened. Every row needs a card of its own. In 1839 a silk firm in Lyon, Didier, Petit and Company, had a portrait of Jacquard woven in silk on a Jacquard loom. It needed about 24,000 cards.
Going further still
Two colors. A real dhaka has red, orange and black. Change weave so the motif can use 'R', 'O' and 'K', with a color for each in show_grid. A Jacquard loom weaves one weft thread at a time; how many cards would a row with three colors need?
Your own rule. Write a rule of your own, like diamond, from r % 8 and c % 8. Can you make a zigzag? A cross? The Nepali flag’s triangle, repeated? (Hint for the zigzag: a slant that goes up for 4 squares and down for 4.)
Mirror a quarter. Many dhaka shapes look the same flipped left to right and upside down. So a weaver, or a program, only needs a quarter of the shape: the rest comes from flipping it. Challenge ★★ below does that.
Challenges
★ Punch the card (blocks)
Finish the program so that it asks for a row number and says the card for that row of the twill, as 8 digits: 1 for a hole (the red thread is lifted, where the remainder of (col + row) ÷ 4 is less than 2), 0 for none. For row 0 it says 11001100. Then press Test.
★★ A diamond from a quarter (Python, from Grade 8)
Write mirror(quarter): given the top-left quarter of a shape (a list of strings), give back the whole shape, flipped left to right and then upside down, with the middle row and middle column used only once. A quarter of 4 rows of 4 gives a shape of 7 rows of 7.
def mirror(quarter): # flip each row to make it twice as wide, then flip the rows return quarter for row in mirror(['...#', '..##', '.#..', '#...']): print(row)★★★ The fewest cards (Python, from Grade 8)
You are given a woven cloth, a list of rows. Write fewest_cards(cloth): the smallest chain of cards that, read round and round from card 1, weaves exactly these rows. (Every card can hold a whole row.)
def fewest_cards(cloth): # the smallest number of cards that weaves these rows in a loop return len(cloth) print(fewest_cards(['#.', '.#', '#.', '.#', '#.']))Questions
In a Jacquard loom, what does a hole in a card do?
Pick an answer to see why.
Why did Babbage want punched cards for his Analytical Engine?
Pick an answer to see why.
A design is 6 rows tall and 10 squares wide. It is woven, every repeat the same, into a cloth 300 rows long and 60 threads wide. How many cards are in the chain, and how many times is each square of the design woven?
Answer
In the program weave, what does motif[r % len(motif)] do, and what would go wrong without the %?
Answer
Explain how a Jacquard loom is like a computer and a program. Give two ways.
Answer
A hand weaver of dhaka makes every choice herself, without a plan. A Jacquard loom follows its cards exactly. Give one thing each does better.
Answer
Where to go next
- Grade 6, Machines with gears and cards: Jacquard, Babbage and Lovelace in the story of the computer.
- Grade 6, Loops inside loops: the loop in a loop that weaves rows of squares.
- Grade 7, Machines that take, change and make jobs: what automation does to work, as the Jacquard loom did to weavers.
- Grade 8, Repeating with for: the loops these Python programs are made of.
- Can One Random Choice Draw a Maze? Another pattern made of squares, this time chosen by chance.
- Why Does a Turtle’s Star Close Up? The turtle again, and patterns for rangoli.
Found a mistake? Please tell us.
