s LearnSqueak.com

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.

8 tested checkpointsNative Squeak screenshotsComputed visual replay

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.

Independent Workspace expression - select all, then Print it
'00123' size

Expected: 5

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Keep leading zeros
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Compare with 123 printString size; that is three.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
$7 asciiValue - $0 asciiValue

Expected: 7

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Turn a character into a digit
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Try $0: its digit value is zero.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| digit |
digit := $8 asciiValue - $0 asciiValue.
digit := digit * 2.
digit > 9 ifTrue: [digit := digit - 9].
digit

Expected: 7

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Fold a doubled digit
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Try $4: doubling gives 8, with no subtraction.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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 asArray

Expected: #(3 2 7 7 9 6 7 4 9 9 7)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Start at the right-hand end
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: The terms sum to 70. Starting by doubling the rightmost digit would use the wrong parity.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Validate a full digit string
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Append a space. It must raise the input error, not pass.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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].
candidate

Expected: 3

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Find a missing check digit
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Use payload 12345; the check digit is 5.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Expose a known limitation
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: This is why a checksum cannot prove correctness, authenticity or ownership.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 8: Test every single-digit mutation
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Single-digit detection is a property of this checksum; passing a checksum is a much weaker claim than being a real identifier.

Download this checkpoint

Watch the computed states

NATIVE SQUEAK CAPTURE
Luhn checksums: detect typing mistakes in digit strings native visual trace
Replay of actual Morphic window captures at successive computed stages; not mouse footage.

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.