SMALLTALK LAB
Pascal triangle: build combinations from neighboring sums
Construct binomial rows with arrays, verify symmetry and powers-of-two totals, and use Pascal’s triangle to count combinations without factorials.
Before you begin
Know Arrays, loops and blocks. Row zero is #(1). Each new row begins and ends with one; every interior cell adds the two cells above it.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
01 - Start with row zero
The top row has one entry. Numbering it zero makes row n contain n+1 entries, matching the coefficients of (a+b) raised to n.
| row |
row := #(1).
row sizeExpected: 1

Try it: Row zero represents the constant polynomial 1.
02 - Compute a single interior sum
For row #(1 3 3 1), the next middle entry adds the two threes. Smalltalk indexing starts at one even though our mathematical row number starts at zero.
| row |
row := #(1 3 3 1).
(row at: 2)+(row at: 3)Expected: 6

Try it: The adjacent sums at the edges are 1+3 and 3+1.
03 - Build the next row in fresh storage
Allocate one extra cell, initialize the boundary ones, then fill interior positions. Do not update the old row in place: later sums must still see its original values.
| row next |
row := #(1 3 3 1).
next := Array new: row size+1 withAll: 1.
2 to: row size do: [:i |
next at: i put: (row at: i-1)+(row at: i)].
nextExpected: {1. 4. 6. 4. 1.}

Try it: Check that row is still #(1 3 3 1).
04 - Package one transition
This block has one responsibility: return the row after the supplied nonempty valid Pascal row. A one-element row has no interior loop iterations and produces two boundary ones.
| nextRow |
nextRow := [:previous | | next |
next := Array new: previous size+1 withAll: 1.
2 to: previous size do: [:i |
next at: i put: (previous at: i-1)+(previous at: i)].
next].
nextRow value: #(1)Expected: {1. 1.}

Try it: Apply it again to get #(1 2 1).
05 - Advance to a requested row
Starting at row zero, execute exactly n transitions. The resulting array is row n. This is iterative dynamic programming: reuse the previous result rather than recursively recompute shared subproblems.
| nextRow row n |
nextRow := [:previous | | next |
next := Array new: previous size+1 withAll: 1.
2 to: previous size do: [:i |
next at: i put: (previous at: i-1)+(previous at: i)].
next].
n := 5. row := #(1).
n timesRepeat: [row := nextRow value: row].
rowExpected: {1. 5. 10. 10. 5. 1.}

Try it: For n = 0 no transitions occur. Validate n as a nonnegative integer in a public API.
06 - Read a combination count
The entry at mathematical column k counts ways to choose k objects from n. Translate column k to Smalltalk index k+1. There are ten pairs among five distinct objects.
| nextRow row |
nextRow := [:previous | | next |
next := Array new: previous size+1 withAll: 1.
2 to: previous size do: [:i |
next at: i put: (previous at: i-1)+(previous at: i)].
next].
row := #(1).
5 timesRepeat: [row := nextRow value: row].
row at: 2+1Expected: 10

Try it: Column zero and column five both count one choice.
07 - Check symmetry and row totals
Every row equals its reversal. Its entries sum to 2 raised to n, because every subset either includes or excludes each of n objects. Test both properties through row twelve.
| nextRow row valid |
nextRow := [:previous | | next |
next := Array new: previous size+1 withAll: 1.
2 to: previous size do: [:i |
next at: i put: (previous at: i-1)+(previous at: i)].
next].
row := #(1). valid := true.
0 to: 12 do: [:n |
valid := valid and: [row = row reversed and: [row sum = (2 raisedTo: n)]].
row := nextRow value: row].
validExpected: true

Try it: Change one interior entry to see which property detects the corruption.
08 - Use the row as polynomial coefficients
Row three is #(1 3 3 1). Weight coefficient k by x raised to k. At x=2 the sum is 27, equal to (1+2) cubed. This connects counting with algebra.
| row total |
row := #(1 3 3 1). total := 0.
0 to: 3 do: [:k | total := total+((row at: k+1)*(2 raisedTo: k))].
totalExpected: 27

Try it: Try x = 1: the result is eight, the row total.
Watch the computed states

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.
Reason about correctness and limits
Computing row n this way performs O(n squared) additions and retains O(n) entries when only the current row is kept. Displaying every row requires more storage. Integer sizes grow, so arithmetic is not constant cost for very large n.
The transition assumes a nonempty Pascal row. It is not a general validator: feeding arbitrary arrays produces neighbor sums but not necessarily binomial coefficients.
Choose counts use zero-based mathematical k but one-based at: indices. Guard 0 <= k <= n when exposing a general choose: API.
Three practice challenges
Challenge 1
Why can you generate only one row at a time?
Show solution
The recurrence uses only the immediately preceding row, not the whole triangle.
Challenge 2
What is the sum of row eight?
Show solution
2 raised to 8, or 256, because it counts all subsets of eight objects.
Challenge 3
Why do the two boundary entries stay one?
Show solution
There is exactly one way to choose none of the objects and exactly one way to choose them all.
Continue learning
Collections | Fractions | Knapsack
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.