SMALLTALK LAB
Luhn checksums: detect typing mistakes in digit strings
Build and test a Luhn checksum in Squeak, trace alternating weights from the right, and generate a check digit for a synthetic identifier.
Before you begin
Use synthetic digit strings only. A checksum detects some transcription mistakes; it is neither authentication nor evidence that an identifier exists. No real account or personal data is needed. The input contract here permits any nonempty ASCII digit string, including a single digit.
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 - Keep leading zeros
Treat identifiers as text, not numbers. Converting to an Integer would erase leading zeros before the checksum even begins.
'00123' sizeExpected: 5

Try it: Compare with 123 printString size; that is three.
02 - Turn a character into a digit
ASCII digits are consecutive. Subtract the code for zero after confirming that the input is an ASCII digit.
$7 asciiValue - $0 asciiValueExpected: 7

Try it: Try $0: its digit value is zero.
03 - Fold a doubled digit
For a doubled value above nine, subtract nine. This is equivalent to summing its two decimal digits: 16 becomes 1+6=7.
| digit |
digit := $8 asciiValue - $0 asciiValue.
digit := digit * 2.
digit > 9 ifTrue: [digit := digit - 9].
digitExpected: 7

Try it: Try $4: doubling gives 8, with no subtraction.
04 - Start at the right-hand end
The rightmost digit is the check digit and is not doubled. Walk left and toggle a Boolean. The returned array is in processing order, from right to left.
| terms double digit |
terms := OrderedCollection new. double := false.
'79927398713' reverseDo: [:c |
digit := c asciiValue - $0 asciiValue.
double ifTrue: [digit := digit * 2.
digit > 9 ifTrue: [digit := digit - 9]].
terms add: digit. double := double not].
terms asArrayExpected: #(3 2 7 7 9 6 7 4 9 9 7)

Try it: The terms sum to 70. Starting by doubling the rightmost digit would use the wrong parity.
05 - Validate a full digit string
Sum the folded terms. A remainder of zero modulo ten means the string passes. Invalid characters raise an error rather than being silently discarded.
| valid |
valid := [:text |
| sum double digit |
(text isEmpty or: [text anySatisfy: [:c | (c between: $0 and: $9) not]])
ifTrue: [Error signal: 'Use a nonempty ASCII digit string'].
sum := 0. double := false.
text reverseDo: [:c |
digit := c asciiValue - $0 asciiValue.
double ifTrue: [digit := digit * 2.
digit > 9 ifTrue: [digit := digit - 9]].
sum := sum + digit. double := double not].
(sum \\ 10) = 0].
{valid value: '79927398713'. valid value: '79927398714'}Expected: #(true false)

Try it: Append a space. It must raise the input error, not pass.
06 - Find a missing check digit
Append each candidate digit and reuse the validator. Testing ten candidates keeps parity correct without maintaining a second implementation.
| valid payload candidate |
valid := [:text |
| sum double digit |
(text isEmpty or: [text anySatisfy: [:c | (c between: $0 and: $9) not]])
ifTrue: [Error signal: 'Use a nonempty ASCII digit string'].
sum := 0. double := false.
text reverseDo: [:c |
digit := c asciiValue - $0 asciiValue.
double ifTrue: [digit := digit * 2.
digit > 9 ifTrue: [digit := digit - 9]].
sum := sum + digit. double := double not].
(sum \\ 10) = 0].
payload := '7992739871'.
candidate := (0 to: 9) detect: [:d |
valid value: payload, d printString].
candidateExpected: 3

Try it: Use payload 12345; the check digit is 5.
07 - Expose a known limitation
The adjacent pair 09 contributes the same amount as 90 at opposite weights. A checksum can therefore miss this swap. Both strings below pass.
| valid |
valid := [:text |
| sum double digit |
(text isEmpty or: [text anySatisfy: [:c | (c between: $0 and: $9) not]])
ifTrue: [Error signal: 'Use a nonempty ASCII digit string'].
sum := 0. double := false.
text reverseDo: [:c |
digit := c asciiValue - $0 asciiValue.
double ifTrue: [digit := digit * 2.
digit > 9 ifTrue: [digit := digit - 9]].
sum := sum + digit. double := double not].
(sum \\ 10) = 0].
{valid value: '109'. valid value: '190'}Expected: #(true true)

Try it: This is why a checksum cannot prove correctness, authenticity or ownership.
08 - Test every single-digit mutation
Starting from the valid synthetic example, replace each of its eleven digits with each of the other nine digits. All 99 changed strings must fail. Also test empty input, spaces and leading zeros.
| valid original changed rejected guards |
valid := [:text |
| sum double digit |
(text isEmpty or: [text anySatisfy: [:c | (c between: $0 and: $9) not]])
ifTrue: [Error signal: 'Use a nonempty ASCII digit string'].
sum := 0. double := false.
text reverseDo: [:c |
digit := c asciiValue - $0 asciiValue.
double ifTrue: [digit := digit * 2.
digit > 9 ifTrue: [digit := digit - 9]].
sum := sum + digit. double := double not].
(sum \\ 10) = 0].
original := '79927398713'. rejected := 0.
1 to: original size do: [:i |
0 to: 9 do: [:d |
(original at: i) asciiValue - $0 asciiValue = d ifFalse: [
changed := original copy.
changed at: i put: (Character value: $0 asciiValue + d).
(valid value: changed) ifFalse: [rejected := rejected + 1]]]].
guards := #('' '12 3' 'abc') allSatisfy: [:s |
[valid value: s. false] on: Error do: [:e | true]].
{rejected. guards. valid value: '0'. valid value: '00'}Expected: #(99 true true true)

Try it: Single-digit detection is a property of this checksum; passing a checksum is a much weaker claim than being a real identifier.
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
The traversal is O(n) time and uses constant extra space. The demonstration stores terms for explanation; the validator does not.
Do not remove spaces or punctuation without an explicit input policy. Rejecting them makes accidental edits visible.
A single zero passes this mathematical validator. Real identifier formats impose separate length and prefix rules; this tutorial makes no such claim.
Three practice challenges
Generate another
Find the check digit for payload 12345.
Show solution
Append 0..9 and test: 123455 passes, so the answer is 5.
Separate concerns
Where should length rules belong?
Show solution
In a separate format validator, followed by the checksum. Keep tests for each rule.
Make an experiment
Replace a digit in 109 and test again.
Show solution
Every different single-digit substitution fails. Swapping 09 to 90 is a two-position change and demonstrates a known exception.
Continue learning
First Character | Blocks | Testing
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Windows.