SMALLTALK LAB
Measure rank disagreement with Kendall distance
Count the pairs of items that change order between two rankings.
Before you begin
Open a Workspace in native Squeak. Select one complete checkpoint and choose Print 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.
Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.
01 - Assign positions in the second ranking
Index the second ranking once. A occurs third and B occurs first, even if the first ranking has A ahead of B.
| b pos |
b := #(b d a c).
pos := Dictionary new.
b withIndexDo: [:item :i | pos at: item put: i].
{pos at: #a. pos at: #b}Expected: #(3 1)

Try it: Look up #d; it is second.
02 - Translate the first ranking
Replace each item in the first ranking with its position in the second. A descending pair now signals disagreement.
| pos |
pos := Dictionary new.
#(b d a c) withIndexDo: [:item :i | pos at: item put: i].
#(a b c d) collect: [:item | pos at: item]Expected: #(3 1 4 2)

Try it: Swap #a and #b in the first array to see positions 1 and 3 switch.
03 - List the reversed pairs
Record the positions of the pairs instead of just counting them. Each Point here stores two indices, not a geometric coordinate.
| hits |
hits := OrderedCollection new.
1 to: 4 do: [:i | i + 1 to: 4 do: [:j |
((#(3 1 4 2) at: i) > (#(3 1 4 2) at: j))
ifTrue: [hits add: i@j]]].
hits asArrayExpected: {1@2. 1@4. 3@4}

Try it: Look up those pairs in #(a b c d).
04 - Count the reversed pairs
Compare each pair exactly once. The reversals are (a,b), (a,d), and (c,d).
| distance |
distance := [:a :b |
| pos count n |
n := a size.
((a asSet size = n) and: [b size = n and: [a asSet = b asSet]])
ifFalse: [Error signal: 'Use two strict rankings of the same items'].
pos := Dictionary new.
b withIndexDo: [:item :i | pos at: item put: i].
count := 0.
1 to: n do: [:i |
i + 1 to: n do: [:j |
((pos at: (a at: i)) > (pos at: (a at: j)))
ifTrue: [count := count + 1]]].
count].
distance value: #(a b c d) value: #(b d a c)Expected: 3

Try it: Swap the two arguments. The count remains 3.
05 - Try the two extremes
Two identical rankings disagree on no pair. Reversing a four-item ranking changes all six pairs.
| distance |
distance := [:a :b |
| pos count n |
n := a size.
((a asSet size = n) and: [b size = n and: [a asSet = b asSet]])
ifFalse: [Error signal: 'Use two strict rankings of the same items'].
pos := Dictionary new.
b withIndexDo: [:item :i | pos at: item put: i].
count := 0.
1 to: n do: [:i |
i + 1 to: n do: [:j |
((pos at: (a at: i)) > (pos at: (a at: j)))
ifTrue: [count := count + 1]]].
count].
{distance value: #(a b c d) value: #(a b c d).
distance value: #(a b c d) value: #(d c b a)}Expected: #(0 6)

Try it: Try reverse order with three items; expect 3.
06 - Catch a duplicate
Repeated labels make the position lookup ambiguous, so stop before indexing.
| distance |
distance := [:a :b |
| pos count n |
n := a size.
((a asSet size = n) and: [b size = n and: [a asSet = b asSet]])
ifFalse: [Error signal: 'Use two strict rankings of the same items'].
pos := Dictionary new.
b withIndexDo: [:item :i | pos at: item put: i].
count := 0.
1 to: n do: [:i |
i + 1 to: n do: [:j |
((pos at: (a at: i)) > (pos at: (a at: j)))
ifTrue: [count := count + 1]]].
count].
[distance value: #(a a b) value: #(a b c)]
on: Error do: [:e | #rejected]Expected: #rejected

Try it: Try a missing item instead; it is also rejected.
Replay the checkpoints

Reason about correctness and limits
This version requires strict rankings of the same distinct items. It counts discordant pairs, not the Kendall tau coefficient.
The position lookup needs O(n) storage. The nested loops compare n(n−1)/2 pairs.
Three practice challenges
Predict one intermediate value
Swap #a and #b in the first array to see positions 1 and 3 switch.
Show solution
The translated positions become #(1 3 4 2).
Work one small variation
Swap the two arguments. The count remains 3.
Show solution
Every pair where the first ranking disagrees with the second also disagrees when their roles are reversed. The count remains 3.
Check the edge of the model
What would go wrong if two items shared the same label?
Show solution
This version requires strict rankings of the same distinct items. It counts discordant pairs, not the Kendall tau coefficient.
Continue learning
Sorting Visualiser | First Sorted
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.