The Paper Machine

Before computers, people ran programs on paper. Pick a program, print its page, and be the machine: go line by line and fill in one row for every step the machine takes: a new value in a box, a check (yes or no), or a print. Some rows are only a check, so some boxes stay empty. When you are done, check your table on the live machine.

Adding two numbers

Reading skill: let + arithmetic · 3 steps

  1. let a = 6
  2. let b = 4
  3. print(a + b)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLineabPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

A box that changes

Reading skill: var reassignment · 4 steps

  1. var score = 3
  2. score = score + 5
  3. score = score * 2
  4. print(score)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinescorePrints
1
2
3
4

Check it on the live machine: sparkanvil.org/play/traceforge

Filling in a sentence

Reading skill: string interpolation · 3 steps

  1. let name = "Ada"
  2. let age = 9
  3. print("\(name) is \(age)")

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinenameagePrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

Sharing that does not divide evenly

Reading skill: Int division truncates · 3 steps

  1. let total = 7
  2. let friends = 2
  3. print(total / friends)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinetotalfriendsPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

Sunny AND warm?

Reading skill: Bool and && (both must be true) · 3 steps

  1. let sunny = true
  2. let warm = false
  3. print(sunny && warm)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinesunnywarmPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

The opposite of the door

Reading skill: Bool and ! (not) · 4 steps

  1. let door = false
  2. let open = !door
  3. print(open)
  4. print(door)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinedooropenPrints
1
2
3
4

Check it on the live machine: sparkanvil.org/play/traceforge

How long is the word?

Reading skill: String .count (how many letters) · 2 steps

  1. let word = "robot"
  2. print(word.count)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinewordPrints
1
2

Check it on the live machine: sparkanvil.org/play/traceforge

Cookies left over

Reading skill: remainder % (what is left over) · 3 steps

  1. let cookies = 17
  2. let perBag = 5
  3. print(cookies % perBag)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinecookiesperBagPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

Two words become one

Reading skill: joining Strings with + · 3 steps

  1. let first = "Sun"
  2. let second = "flower"
  3. print(first + second)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinefirstsecondPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

A swap that goes wrong

Reading skill: var reassignment, in order · 6 steps

  1. var a = 2
  2. var b = 5
  3. a = b
  4. b = a
  5. print(a)
  6. print(b)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLineabPrints
1
2
3
4
5
6

Check it on the live machine: sparkanvil.org/play/traceforge

Sharing with decimals

Reading skill: Double division keeps the fraction · 3 steps

  1. let total = 7.0
  2. let friends = 2.0
  3. print(total / friends)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinetotalfriendsPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

Making a new box from an old one

Reading skill: let: a name keeps its value · 5 steps

  1. let x = 4
  2. print(x)
  3. let y = x + 1
  4. print(y)
  5. print(x)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinexyPrints
1
2
3
4
5

Check it on the live machine: sparkanvil.org/play/traceforge

Which branch runs?

Reading skill: if / else · 3 steps

  1. let n = 8
  2. if n % 2 == 0 {
  3. print("even")
  4. } else {
  5. print("odd")
  6. }

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinenPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

Counting with ..<

Reading skill: for-in ..< (exclusive) · 7 steps

  1. for i in 1..<4 {
  2. print(i)
  3. }

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLineiPrints
1
2
3
4
5
6
7

Check it on the live machine: sparkanvil.org/play/traceforge

Counting with ...

Reading skill: for-in ... (inclusive) · 9 steps

  1. var sum = 0
  2. for i in 1...3 {
  3. sum = sum + i
  4. }
  5. print(sum)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinesumiPrints
1
2
3
4
5
6
7
8
9

Check it on the live machine: sparkanvil.org/play/traceforge

How long, and which one?

Reading skill: arrays: .count + index · 3 steps

  1. let pets = ["cat", "dog", "fish"]
  2. print(pets.count)
  3. print(pets[0])

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinepetsPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

Hot, mild or cold?

Reading skill: if / else if / else · 4 steps

  1. let t = 15
  2. if t > 20 {
  3. print("hot")
  4. } else if t > 10 {
  5. print("mild")
  6. } else {
  7. print("cold")
  8. }

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinetPrints
1
2
3
4

Check it on the live machine: sparkanvil.org/play/traceforge

A loop that visits each word

Reading skill: for-in over an array · 8 steps

  1. let words = ["red", "fox", "ran"]
  2. for w in words {
  3. print(w.count)
  4. }

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinewordswPrints
1
2
3
4
5
6
7
8

Check it on the live machine: sparkanvil.org/play/traceforge

Packing a bag

Reading skill: arrays: .append adds to the end · 5 steps

  1. var bag: [String] = []
  2. bag.append("pen")
  3. bag.append("map")
  4. print(bag.count)
  5. print(bag[1])

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinebagPrints
1
2
3
4
5

Check it on the live machine: sparkanvil.org/play/traceforge

Fixing one score

Reading skill: arrays: changing one item · 3 steps

  1. var scores = [4, 7, 2]
  2. scores[1] = 9
  3. print(scores)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinescoresPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

How many are even?

Reading skill: counting with if inside a loop · 18 steps

  1. var evens = 0
  2. for n in 1...6 {
  3. if n % 2 == 0 {
  4. evens = evens + 1
  5. }
  6. }
  7. print(evens)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLineevensnPrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Check it on the live machine: sparkanvil.org/play/traceforge

Getting louder

Reading skill: building a String in a loop · 9 steps

  1. var s = "go"
  2. for i in 1...3 {
  3. s = s + "!"
  4. }
  5. print(s)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinesiPrints
1
2
3
4
5
6
7
8
9

Check it on the live machine: sparkanvil.org/play/traceforge

Can they ride?

Reading skill: Bool || (or) in a test · 4 steps

  1. let age = 10
  2. let withAdult = true
  3. if age >= 12 || withAdult {
  4. print("ride")
  5. } else {
  6. print("wait")
  7. }

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLineagewithAdultPrints
1
2
3
4

Check it on the live machine: sparkanvil.org/play/traceforge

One, few or many?

Reading skill: switch picks one case · 3 steps

  1. let n = 2
  2. switch n {
  3. case 1:
  4. print("one")
  5. case 2, 3:
  6. print("few")
  7. default:
  8. print("many")
  9. }

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinenPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

Jumping down by threes

Reading skill: while counting down past zero · 11 steps

  1. var n = 10
  2. while n > 0 {
  3. n = n - 3
  4. }
  5. print(n)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinenPrints
1
2
3
4
5
6
7
8
9
10
11

Check it on the live machine: sparkanvil.org/play/traceforge

Adding from zero

Reading skill: for-in ..< starting at 0 · 13 steps

  1. var total = 0
  2. for i in 0..<5 {
  3. total = total + i
  4. }
  5. print(total)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinetotaliPrints
1
2
3
4
5
6
7
8
9
10
11
12
13

Check it on the live machine: sparkanvil.org/play/traceforge

Reading a function call

Reading skill: func with return · 4 steps

  1. func triple(x: Int) -> Int {
  2. return x * 3
  3. }
  4. print(triple(x: 4) + 1)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinetriple: xtriple returnsPrints
1
2
3
4

Check it on the live machine: sparkanvil.org/play/traceforge

A loop inside a loop

Reading skill: nested for-in · 13 steps

  1. for a in 1...2 {
  2. for b in 1...2 {
  3. print("\(a)\(b)")
  4. }
  5. }

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLineabPrints
1
2
3
4
5
6
7
8
9
10
11
12
13

Check it on the live machine: sparkanvil.org/play/traceforge

Building up an answer

Reading skill: accumulate over an array · 11 steps

  1. let nums = [3, 1, 4]
  2. var best = nums[0]
  3. for n in nums {
  4. if n > best {
  5. best = n
  6. }
  7. }
  8. print(best)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinenumsbestnPrints
1
2
3
4
5
6
7
8
9
10
11

Check it on the live machine: sparkanvil.org/play/traceforge

Stopping at the right time

Reading skill: while + condition · 19 steps

  1. var n = 1
  2. var steps = 0
  3. while n < 20 {
  4. n = n * 2
  5. steps = steps + 1
  6. }
  7. print(steps)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinenstepsPrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Check it on the live machine: sparkanvil.org/play/traceforge

Inside out

Reading skill: two functions, one call inside another · 7 steps

  1. func double(x: Int) -> Int {
  2. return x * 2
  3. }
  4. func addOne(x: Int) -> Int {
  5. return x + 1
  6. }
  7. print(addOne(x: double(x: 5)))

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinedouble: xdouble returnsaddOne: xaddOne returnsPrints
1
2
3
4
5
6
7

Check it on the live machine: sparkanvil.org/play/traceforge

Two boxes called x

Reading skill: a function’s own box (the same name) · 6 steps

  1. let x = 1
  2. func bump(x: Int) -> Int {
  3. return x + 10
  4. }
  5. print(bump(x: 5))
  6. print(x)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinexbump: xbump returnsPrints
1
2
3
4
5
6

Check it on the live machine: sparkanvil.org/play/traceforge

Countdown trays

Reading skill: recursion: a function that calls itself · 21 steps

  1. func countdown(n: Int) {
  2. if n == 0 {
  3. print("Go!")
  4. return
  5. }
  6. print(n)
  7. countdown(n: n - 1)
  8. }
  9. countdown(n: 3)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinecountdown: ncountdown #2: ncountdown #3: ncountdown #4: nPrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Check it on the live machine: sparkanvil.org/play/traceforge

Trays that hand back

Reading skill: recursion that hands values back · 17 steps

  1. func fact(n: Int) -> Int {
  2. if n <= 1 {
  3. return 1
  4. }
  5. return n * fact(n: n - 1)
  6. }
  7. print(fact(n: 4))

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinefact: nfact #2: nfact #3: nfact #4: nfact returnsPrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Check it on the live machine: sparkanvil.org/play/traceforge

A pattern, one letter at a time

Reading skill: if / else inside a loop · 15 steps

  1. var line = ""
  2. for i in 1...4 {
  3. if i % 2 == 0 {
  4. line = line + "x"
  5. } else {
  6. line = line + "o"
  7. }
  8. }
  9. print(line)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinelineiPrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Check it on the live machine: sparkanvil.org/play/traceforge

Meeting in the middle

Reading skill: while with two changing boxes · 17 steps

  1. var a = 0
  2. var b = 10
  3. while a < b {
  4. a = a + 2
  5. b = b - 1
  6. }
  7. print(a)
  8. print(b)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLineabPrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Check it on the live machine: sparkanvil.org/play/traceforge

Copying backwards

Reading skill: walking an array backwards · 14 steps

  1. let nums = [1, 2, 3]
  2. var out: [Int] = []
  3. var i = nums.count - 1
  4. while i >= 0 {
  5. out.append(nums[i])
  6. i = i - 1
  7. }
  8. print(out)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinenumsoutiPrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Check it on the live machine: sparkanvil.org/play/traceforge

Adding squares

Reading skill: calling a function inside a loop · 18 steps

  1. func square(n: Int) -> Int {
  2. return n * n
  3. }
  4. var total = 0
  5. for i in 1...3 {
  6. total = total + square(n: i)
  7. }
  8. print(total)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinetotalisquare: nsquare returnsPrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Check it on the live machine: sparkanvil.org/play/traceforge

A box with a label inside

Reading skill: a struct holds named boxes · 3 steps

  1. struct Jar {
  2. var count: Int
  3. }
  4. var jar = Jar(count: 2)
  5. jar.count = jar.count * 3
  6. print(jar.count)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinejarPrints
1
2
3

Check it on the live machine: sparkanvil.org/play/traceforge

A program you didn’t write

Reading skill: reading a program you didn’t write · 25 steps

  1. let rolls = [4, 6, 2, 6, 5]
  2. var total = 0
  3. var sixes = 0
  4. for r in rolls {
  5. total = total + r
  6. if r == 6 {
  7. sixes = sixes + 1
  8. }
  9. }
  10. let average = total / rolls.count
  11. if sixes >= 2 {
  12. print("lucky")
  13. } else {
  14. print("plain")
  15. }
  16. print(average)

One row per step: a new value, a check (yes or no), or a print.

Your trace table
StepLinerollstotalsixesraveragePrints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Check it on the live machine: sparkanvil.org/play/traceforge