Skip to content

← Creating

How Does a Loom Follow a Program?

Topic 31 · Grades 6–8 · Patterns as programs

Blocks, from Grade 6 (Programming with Blocks) and Grade 7 (variables); the Python parts from Grade 8 (loops and lists).

What this builds on from class
In this unit 8 sections
  1. Try it
  2. How it works
  3. Program it
  4. What your program shows
  5. Going further still
  6. Challenges
  7. Questions
  8. Where to go next

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.

A Nepali dhaka topi lying flat, its cloth covered in rows of small repeating flowers, steps and zigzags in orange, pink, green and black on white
Figure 31.1 A dhaka topi. Look for a shape, then find it again: the same few rows of choices, repeated. Photo: Ramnam, CC BY-SA 3.0

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.

Try it

A loom that reads cards

Tap a square of the design to turn it red or back. Watch the card beside that row change, and the cloth below. Press Weave to see the loom read its cards row by row.

Your design: tap a squareIts cardsThe cloth: 48 threads across, 24 rows
Start from:
Repeats:
Cards in the chain
8
Holes punched
28
Squares of cloth
1,152

Each card is one row of the design: a hole lifts the red thread there. The loom reads the cards in turn, then starts again from the first.

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.

griffe riseshooksneedlescardwarpweft →clothRed shows where a thread was lifted.
Figure 31.2 How a Jacquard machine reads one card: one row of 8 hooks, seen from the side, as Thomas Posselt drew it in 1888. The card is pressed against the needles’ heads. Where it has a hole, the needle goes in, its hook stays over its knife on the griffe, and as the griffe rises the hook is lifted with its warp thread. Where there is no hole, the card pushes the needle back, the needle pushes its hook off the knife, and that thread stays down. The weft passes between.

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 top of a wooden Jacquard loom: on the left hundreds of cords fan out downwards, on the right a long chain of punched cards hangs in folds
Figure 31.3 A Jacquard loom in the National Museum of Scotland. On the right hangs its chain of punched cards, laced together; on the left, the cords that run down from the hooks to the warp threads. Photo: Stephencdickson, CC BY-SA 4.0

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.

Try it

Weave with the turtle

Press Run and watch it weave 12 rows. Then change the 2 to a 3 and run it again. Then try the twill below.

What the program says and asks appears here.

row – no value
col – no value

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

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 holes

Card 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 all

Picture

A cloth of 24 rows and 48 squares: hollow red diamonds on cream, 6 across and 3 down, touching at their corners.

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

Plain weave: a fine checkerboard of red and cream squares.
Twill: red and cream stripes two squares wide, running diagonally.
Diamond: red diamond outlines, each with a small red diamond in its middle, repeating in rows.

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 times

That 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.

A black-and-white portrait of Joseph Marie Jacquard seated at a workbench with tools and a model of his loom, so finely woven in silk that it looks like an engraving
Figure 31.4 The portrait of Jacquard woven in silk in 1839, from about 24,000 punched cards. It looks like an engraving, but it is cloth. Photo: Popular Graphic Arts, Public domain

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.

Try it

★ Punch the card

What the program says and asks appears here.

row – no value
card – no value
col – no value
TestShould sayIt said
Row 011001100
Row 110011001
Row 200110011
Row 301100110
Row 910011001

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

Hint
Answer

★★ 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)

Try it

★★ A diamond from a quarter

  1. A diamond from 4 rows of 4 mirror(['...#', '..##', '.#..', '#...']) Should give ['...#...', '..###..', '.#...#.', '#.....#', '.#...#.', '..###..', '...#...'] Yours —
  2. Two rows of two mirror(['#.', '.#']) Should give ['#.#', '.#.', '#.#'] Yours —
Tests passed
— / 7
Challenge
★★ solved

Write your mirror in the program above (Edit), then check it.

A test your function gets wrong, anything it prints, and any error appear here.

Hint
Another hint
Answer

★★★ 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(['#.', '.#', '#.', '.#', '#.']))

Try it

★★★ The fewest cards

  1. Plain weave, 5 rows fewest_cards(['#.', '.#', '#.', '.#', '#.']) Should give 2 Yours —
  2. The diamond cloth, 24 rows fewest_cards(['...##......##...', '..####....####..', '.##..##..##..##.', '##....####....##', '##....####....##', '.##..##..##..##.', '..####....####..', '...##......##...', '...##......##...', '..####....####..', '.##..##..##..##.', '##....####....##', '##....####....##', '.##..##..##..##.', '..####....####..', '...##......##...', '...##......##...', '..####....####..', '.##..##..##..##.', '##....####....##', '##....####....##', '.##..##..##..##.', '..####....####..', '...##......##...']) Should give 8 Yours —
Tests passed
— / 11
Challenge
★★★ solved

Write your fewest_cards in the program above (Edit), then check it.

A test your function gets wrong, anything it prints, and any error appear here.

Hint
Answer

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.

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
b.

In the program weave, what does motif[r % len(motif)] do, and what would go wrong without the %?

Answer
c.

Explain how a Jacquard loom is like a computer and a program. Give two ways.

Answer
d.

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

Found a mistake? Please tell us.