# Knitting and weaving

From A Little Lisp for a Small World, the Plazmi Lisp book (https://plazmi.gg/book). Checked against Plazmi Lisp version 13, 19 November 2026. The whole book as one file: https://plazmi.gg/book.md?rev=4d22859c86a27cff3b4c4ae5

A knitting pattern is a function: give it a row and a stitch, and it says which stitch goes there, knit or purl, and in which colour. A woven one is two lists, the colours of the threads going one way and the threads going the other. This chapter writes both, and knits a scarf with them.

## The repeat

Every pattern here is a repeat: a small block of stitches, at most 8 rows by 8 stitches, tiled along a scarf 12 stitches wide. Stripes, ribs, seed stitch, checks, chevrons and tartans are all repeats that small. A picture or a word isn't, so a scarf never carries one: the repeat is the whole of what a pattern can say. A scarf has up to 3 colours, by name from the paint box, numbered from 0.

There are two weights. A knitted scarf is chunky, with ridges you can see from the gate. A woven one is fine, its threads crossing in a twill, with a twisted fringe at each end.

## A chart, written out

`knit` takes a name, a map of colours, and the chart: one text a row, its stitches by spaces. `k` is a knit stitch and `p` a purl. Seed stitch is a knit and a purl, swapped on the next row:

```clojure
(knit "seed" {:colours ["moss green"]} ["k p" "p k"])
```

```text
A pattern in your pattern book: seed. hobby (pattern: seed) knits scarves in it.
```

A stitch in another colour has the colour's number after it: `k1` is a knit stitch in the second colour, `p2` a purl in the third. Four rows of each colour make stripes, and knitting one row and purling the next makes a ridge every second row, which is garter stitch:

```clojure
(knit "harbour stripes" {:colours ["navy" "cream"]}
      ["k" "p" "k" "p" "k1" "p1" "k1" "p1"])
```

```text
A pattern in your pattern book: harbour stripes. hobby (pattern: harbour stripes) knits scarves in it.
```

A row of one stitch is that stitch all the way across. A row of four, `"k k p p"`, is repeated across the scarf's twelve: a rib.

## A chart, worked out

A chart is text, and text can be worked out, as a design's rows are in "Pictures and dances". The stitch is a function of the row and the stitch, both counted from 0, and the chart is that function asked once for every place in the repeat. `check` holds the worked-out fields to the same rules as when they're written out, and `apply` hands them to `knit`:

```clojure
(defn stitch [row st]
  (if (even? (+ row st)) "k" "p"))
(defn chart [rows stitches]
  (mapv (fn [r] (join " " (map (fn [s] (stitch r s)) (range stitches)))) (range rows)))
(apply knit (check "moss" :knit {:name "moss" :colours ["olive"] :rows (chart 2 2)}))
```

```text
A pattern in your pattern book: moss. hobby (pattern: moss) knits scarves in it.
```

Change `stitch` and the pattern changes with it. A chevron is a stitch whose colour depends on how far it is from the middle of the row:

```clojure
(defn stitch [row st]
  (if (< (mod (+ row (abs (- st 4))) 4) 2) "k" "k1"))
(defn chart [rows stitches]
  (mapv (fn [r] (join " " (map (fn [s] (stitch r s)) (range stitches)))) (range rows)))
(apply knit (check "chevron" :knit {:name "chevron" :colours ["mustard" "navy"] :rows (chart 4 8)}))
```

```text
A pattern in your pattern book: chevron. hobby (pattern: chevron) knits scarves in it.
```

## The function itself

With a key, `knit` takes the function in the chart's place, and the size of its repeat in the map: `:rows` and `:stitches`, each up to 8. It is asked for every stitch of the repeat, and gives `:knit` or `:purl`, `[:knit 1]` for a stitch in another colour, or just a colour's number for a knit stitch in it:

```clojure
(knit "chevron" {:colours ["mustard" "navy"] :rows 4 :stitches 8}
      (fn [row st] (if (< (mod (+ row (abs (- st 4))) 4) 2) :knit [:knit 1])))
```

```text
A new pattern in your pattern book, "chevron": knitted, chunky, 4 rows by 8 stitches, in mustard and navy.
row 1: …
…
knit with chevron puts it on the needles.
```

The reply shows the chart the function gave. The chart is what's kept: the function has done its work once the chart is made.

## A weave

`weave` takes a name and a map: the colours, the `:warp` (the colour of each band of threads running along the scarf) and the `:weft` (each band running across it). Left out, the weft is the warp's own, and a warp crossed by itself is a tartan:

```clojure
(weave "fireside" {:colours ["taupe" "rust" "navy"] :warp [0 0 0 1 0 0 2 2]})
```

```text
A pattern in your pattern book: fireside. hobby (pattern: fireside) knits scarves in it.
```

Two colours, one band each, make gingham. A weft unlike the warp makes stripes one way and checks the other, and `:fringe false` leaves the ends plain:

```clojure
(weave "picnic" {:colours ["cream" "barn red"] :warp [0 1] :weft [0 1]})
(weave "deckchair" {:colours ["cream" "sea blue"] :warp [0 1 1] :weft [0] :fringe false})
```

```text
A pattern in your pattern book: deckchair. hobby (pattern: deckchair) knits scarves in it.
A pattern in your pattern book: picnic. hobby (pattern: picnic) knits scarves in it.
```

## What doesn't fit

A repeat bigger than 8 by 8 is refused, and the reply says why:

```clojure
(knit "portrait" {:colours ["navy"]}
      ["k" "k" "k" "k" "k" "k" "k" "k" "k" "k"])
```

```text
… A pattern is a repeat of at most 8 rows by 8 stitches, tiled along a scarf 12 stitches wide: stripes, ribs, seed stitch, checks, chevrons and tartans fit; pictures and words don't. 10 rows is more than the repeat holds. …
```

So is a fourth colour:

```clojure
(weave "rainbow" {:colours ["rose" "mustard" "sage" "sea blue"] :warp [0 1 2 2]})
```

```text
… A scarf has up to 3 colours; that's 4. …
```

## Knitting it

A pattern is knitted with the hobby. `knit with` and a pattern's name puts it on the needles: the scarf in hand carries on in it, and each scarf after, until another is chosen. The hobby moves it on in quiet moments, and `hobby work` is a sitting with it. Finished, a scarf is yours: `scarves` lists them, `wear my scarf` puts one on, and `give my scarf to` and a name gives one to a neighbour, to keep.

```text
> knit with seed
… You cast on a seed scarf: knitted, chunky, 2 rows by 2 stitches, in moss green. …
> patterns
Your pattern book:
…
```

The library has a few to begin with, anyone's to knit: garter, rib, seed, stripes, basketweave, tartan and gingham. `knit with tartan in navy, cream and rust` knits one in colours of your own, as many as the pattern has.

Patterns are for sharing. A neighbour reads yours with `patterns` and your name, and one of them with `pattern`, your name and its name (`pattern juniper's fireside`), which shows its chart and its Lisp. `knit with juniper's fireside` knits it, and the scarf says whose pattern it was.

## Try it

1. Write a 2 by 2 rib: two knit stitches and two purls, every row.
2. Write a stitch function for a checkerboard of two colours, in blocks of four, and work its chart out with `check`.
3. Weave a tartan of your own from three colours, with a weft unlike its warp.
4. Try a repeat of 9 rows, and read what the reply says.

## Recap

- A pattern is a repeat of at most 8 rows by 8 stitches, in up to 3 colours, tiled along a scarf 12 stitches wide.
- `knit` takes a chart, or with a key a function of row and stitch; `weave` takes a warp and a weft.
- `knit with` a pattern knits scarves in it; `scarves`, `wear my scarf` and `give my scarf to` do the rest.

That is the end of the tutorial. Part IV is the reference: every word, every limit, and the answers.


---

Before: [Style](https://plazmi.gg/book/style.md). Next: [The word table](https://plazmi.gg/book/reference.md). Contents: https://plazmi.gg/book.md?rev=4d22859c86a27cff3b4c4ae5
