Knitting as Programming

a knit octopus sweater in blue and white
Knit by Abbey Perini, pattern is Embrace Octopus Sweater by Maia E. Sirnes

The Beginning

Fiber arts, like knitting, are crafts using yarn, string, thread, etc. This encompasses thousands of years of history, starting in 6500 BCE with Viking Nålbinding. Other crafts included under this umbrella are crochet, embroidery, weaving, and textile making.

a woman weaves wire to create core rope memory


Like computers, the textile industry has come a long way since the Jacquard Loom, integrating more and more automated manufacturing. Perhaps you’d like to 3D print a lot of knit toys? Makers will find a way, so when home knitting machines were no longer produced, people built the system themselves.

A Knitting Primer

When you knit something, you use two knitting needles. All of your stitches will be live, meaning if you drop a stitch the fabric you’re creating will unravel. Patterns are created by alternating types of stitches or yarn of different colors. For example, K2P2 would mean 2 knit stitches followed by 2 purl stitches. The difference between stitches is how you feed the new loop of yarn through the live stitch on your needle. Effectively, a knit stitch goes through the front of the loop. A purl stitch goes through the back of the loop. Repeating this for several rows is how you would get a ribbed brim on a hat.

a diagram of K2P2 ribbing
K2P2 Ribbing

Patterns as Programming

  • Knitting pattern designers follow DRY.
  • Pattern repeats are for loops, typically surrounded by brackets, asterisks, or parentheses.
  • It’s common to see while loops like ‘until the end of the round’ or ‘to n stitches before marker’.
  • Higher level languages like Python and Javascript use symbols and keywords to tell the computer what the desired output is, just like knitting charts.
  • Like assembly languages, operands are pushed and popped onto the needles.
  • Patterns are tested and retested by users for correctness, scalability, and the look of the finished object (aka expected output).
  • Pattern developers prefer open source software (Darktable, Gimp, Libre, Stitchmastery, etc.).
  • Different placements of yarn and needles produce different outputs.
a lacey Dowland shawl knit in sparkly burgundy yarn
Knit by Abbey Perini, pattern by Dowland by Dee O’Keefe, yarn is Meeker Street by The Jewelry Box

Patterns as Regular Expressions

Say we want to make a square of ribbing. kp is the regular expression for one knit stitch followed immediately by one purl stitch. Curly brackets are used for repeats, so kp repeated 10 times looks like this: (kp){10}. For a new row we use a new line, written, \n. The full regular expression for the row is then (kp){10}\n. Since we turn the square to knit the next row, the following line must be pks, or (pk){10}\n. We want our square to be 40 rows, so they are in turn wrapped up in yet more brackets, producing: ((kp){10}\n(pk){10}\n){20}. A more general pattern would look like ((kp){x}\n(pk){x}\n){y}.

Patterns as Tools for Teaching Programming


Having taught myself programming and knitting and crochet, I can safely say the mindset required is very similar. From figuring out how to make a project work as you go to regular expressions in patterns, there are a lot of parallels. Programming as it is today would not exist without fiber, and programmers could learn a thing or two about teaching beginners from the online knitting community.

Additional Fun

Knitting is Coding and Yarn Is Programmable in This Physics Lab

Additional Sources



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abbey Perini

Abbey Perini

…did someone say animated CSS button?