Chapter 0: Hello World

When people learn to knit, they might learn to knit a garter stitch rectangle. They cast on a number of stitches (say 24), knit for a number of rows (say 24), and then bind off. This is about as simple as a knitted item can be; it does have casting on and binding off, but in between it only has “k” stitches. When finished, it can be used to calculate the gauge you are getting with your yarn and your needle size in garter stitch, or it can be used as a dishcloth or a doll blanket (depending on your type of yarn.)

When people learn a new computer language, their traditional “doll blanket” is to learn how to make the computer print the words “Hello World” on the computer screen by using commands in that computer language. This is about as simple as a program can be while still being able to show that it’s doing anything at all.

We will write a simple pattern for a garter stitch gauge swatch. We will name this simple pattern “Hello World” out of nostalgia for the tradition of programmers (though we could just as easily name it “Doll Blanket”.)

Let’s consider the meat of the project first (just like a new knitter who has had someone else cast on for them, we wil come back to the beginning and ending parts in a minute.) How do we say “knit every stitch in this row”? Then, when we have said that, how do we say “repeat this row so-many times” or better yet “repeat this row until your work measures so-many inches/centimeters/whatever”?

Here is how we say “knit every stitch in this row”:

Row: k to end

We can also write it this way, if we have something longer to write than “k to end” and want to be able to use more than one line.

Row {
  k to end
}

It is also ok to write out the word “knit” instead of just “k”.

Row: knit to end

When we are describing a repeated pattern to someone, we say things like “this row” or “rows 1-5″ or “keep going in pattern”. Humans can usually figure out which row is “this” or what “in pattern” means. This is harder for a computer to do. We will make it easier by giving a name to the repeated part (like “Bob”) and referring to it by that name. How do we give a name to our “k to end” row?

We put it inside an “Instruction” like this. An Instruction is like a
“hello my name is __” nametag.

Instruction 'Bob' {
  Row: k to end
}

Instead of “Bob” of course we want to use a name that describes it very well… not because the computer cares, but because humans might want to look at our encoded pattern too and understand it quickly.

You could describe what you are doing (lots of “K”),

Instruction 'plain-knitting-row' {
  Row: k to end
}

Or describe the name of the stitch pattern,

Instruction 'garter-stitch' {
  Row: k to end
}

Or describe the part of the garment/article that you would use this stitch in,

Instruction 'doll-blanket-main-part' {
  Row: k to end
}

Let’s suppose we are calling it ‘garter-stitch’. This name is the one we could get the most re-use from because we will not always be knitting doll blankets but will often want to paste a bit of garter stitch into a pattern. (Plain-knitting-row is ok, but we can see by looking at “k to end” that it is plain knitting and so to a human this name choice is not so useful.) You should always think “reduce, re-use, recycle” when you are writing computer programs to save yourself future work.

Instruction 'garter-stitch' {
  Row: k to end
}

Now that we have settled on a name, let’s learn how to repeat what we have named. Suppose we want 24 rows… we have already done one and it would be natural to say “do this 23 more times.”

Instruction 'garter-stitch' {
  Row: k to end
}
Repeat 'garter-stitch' 23 additionalTimes

Instead of a number of rows, maybe we would prefer to describe how long the work should measure in inches or cm:

Instruction 'garter-stitch' {
  Row: k to end
}
Repeat 'garter-stitch' until measures 4 in

What if we wanted a stockinette swatch instead of garter? In stockinette we knit one row, then purl one row, and repeat these two rows. We’ll change the name from ‘garter-stitch’ so that a human will understand what is going on (the computer wouldn’t care if we lied and said it was still called garter, or even if we called it “Bob”.)

Instruction 'stockinette-st' {
  Row: k to end
  Row: p to end
}
Repeat 'stockinette-st' until measures 4 in

Now you know how to describe a block of garter or stockinette. It’s time to learn how to begin and end.

Suppose we start by casting on 24 sts with a long tail cast on.

CastOn 24 'long-tail'
Instruction 'garter-stitch' {
  Row: k to end
}
Repeat 'garter-stitch' until measures 4 in

At the end we will add one last row in which we bind off.

Row: BindOff all sts

We need to wrap all of this inside a “Directions”

Directions {
  CastOn 24 'long-tail'
  Instruction 'garter-stitch' {
    Row: k to end
  }
  Repeat 'garter-stitch' until measures 4 in
  Row: BindOff all sts
}

Remember that for every open brace “{” we need a matching close brace “}”. If we indent the text by a couple more spaces every time we open a brace, and carry on indenting until we close a brace, it is easier for humans to read and to see whether the matching close braces are where they need to be. Computers don’t care about the indenting.

In a real pattern there are other things we need to describe in addition to the directions themselves. Can you think of some things that are usually listed at the beginning of a pattern?

  • gauge you should be getting
  • what kind of yarn and how much is needed
  • size of needles the designer used
  • any notions required
  • various other things like a name and description for the pattern

Some of these things will be described in “Supplies { … }”, such as the yarn, needles, and notions, and the rest will be described in “GeneralInformation { … }”. We will put these things before our “Directions { … }”. Here is a simple example of GeneralInformation and Supplies:

GeneralInformation {
  Name: 'Hello World'
  Gauge 'stockinette' {
    StitchGauge: 6 stitchesPerInch
    RowGauge: 6 rowsPerInch
  }
}

The main things to notice in GeneralInformation are the name that we gave our pattern (Hello World) and the stockinette gauge that we are specifying (6 st/in and 6 rows/in.) You would want to change these things for different patterns.

Supplies {
  Yarns {
    YarnType 'my-totally-awesome-yarn' [weight:'worsted']
  }
  Needles {
    NeedleType 'my-favorite-needles' circular { size: 7 US }
    Needle 'the-only-needles-i-am-using' [typeref:'my-favorite-needles']
  }
}

You can describe a lot of things in Supplies. I won’t go into it right now, because you can read about it in the User’s Guide, but I will talk about what this example is describing. Here we are naming a worsted-weight yarn “my-totally-awesome-yarn” in case we want to refer to it later; a project might have more than one kind of yarn and we would need to be able to say which one we’re switching to when we switch yarns, so they would need names.

The same thing goes for needles. We name the needle that we are using, in case we want to switch to a different kind of needle or use multiple needles at the same time (for example people might use smaller needles for ribbed cuffs in a sweater, or might switch from a circular needle to a set of same-size double pointed needles for the decreases in the crown of a beret; people also often use two circular needles at once to knit in the round and may need to distinguish heel/instep, front/back etc.) Before naming the needle itself, we first named the *type* of needle (that is, the size and shape of the needle) so that we can refer to the type of needle by name when we are naming the needle we use in the pattern.

Again, think about “reuse” – if we write a lot of patterns that use similar weights of yarn and similar needles (as a sock designer might, for example), we can pretty much reuse the whole Supplies block from a previous pattern, so it’s very little work in the long run.

Now we are ready to put the whole pattern together: GeneralInformation, Supplies, and Directions. We wrap these three parts inside a “Pattern” and specify that we want it in English (’en’)

Pattern 'en' {
  Generalinformation {
    Name: 'Hello World'
    Gauge 'stockinette' {
      StitchGauge: 6 stitchesPerInch
      RowGauge: 6 rowsPerInch
    }
  }
  Supplies {
    Yarns {
      YarnType 'my-totally-awesome-yarn' [weight:'worsted']
    }
    Needles {
      NeedleType 'my-favorite-needle-size' circular { size: 7 US }
      Needle 'the-only-needles-i-am-using' [typeref:'my-favorite-needle-size']
    }
  }
  Directions {
    CastOn 24 'long-tail'
    Instruction 'garter-stitch' {
      Row: k to end
    }
    Repeat 'garter-stitch' until measures 4 in
    Row: BindOff all sts
  }
}

It seems like a disproportionate amount of typing just to describe a garter stitch rectangle. However for a more complex knitted item, the Directions will soon outweigh the other parts of the specification.

To make a simple knitted rectangle with some other stitch, such as stockinette, we change the Directions just as we did before, leaving everything else the same:

Pattern 'en' {
  Generalinformation {
    Name: 'Hello World'
    Gauge 'stockinette' {
      StitchGauge: 6 stitchesPerInch
      RowGauge: 6 rowsPerInch
    }
  }
  Supplies {
    Yarns {
      YarnType 'my-totally-awesome-yarn' [weight:'worsted']
    }
    Needles {
      NeedleType 'my-favorite-needle-size' circular { size: 7 US }
      Needle 'the-only-needles-i-am-using' [typeref:'my-favorite-needle-size']
    }
  }
  Directions {
    CastOn 24 'long-tail'
    Instruction 'stockinette-st' {
      Row: k to end
      Row: p to end
    }
    Repeat 'stockinette-st until measures 4 in
    Row: BindOff all sts
  }
}

TERMS

Each chapter will contain a (rather hasty at present) list of the terms in Appendix B. Knitting Expression Language Reference that have been introduced in that chapter.

bindOff
castOn
directions
generalInformation (containing these things: name, gauge, stitchGauge, rowGauge)
instruction
K, Knit (as well as: k to end)
P
pattern (containing these things: generalInformation, supplies, directions)
row
repeat (so far we’ve seen: repeat until measures)
supplies (containing these things: yarns, yarnType, needles, needletype, needle)