ISBN Verifier
Decide whether a string is a valid ISBN-10, with or without dashes.
Adapted from Exercism’s problem specification (MIT). Listed under Parsing & Validation.
// FILE: isbn_verifier.nomi
//# Checks ISBN-10 numbers.
/// Reports whether `isbn` is a valid ISBN-10: nine digits and a check
/// character, a digit or `X` for 10, with dashes allowed anywhere, whose
/// digits weighted 10 down to 1 sum to a multiple of 11.
pub fn is_valid(isbn: String) -> Bool
values =
isbn
|> String.replace("-", "")
|> Iter.with_index()
|> Iter.map(fn (i, character) -> value(character, is_check: i == 9))
|> Maybe.collect()
case values
Some(values) when Iter.count(values) == 10 -> checksum(values) % 11 == 0
_ -> False
end
end
/// The value of one ISBN character: a digit, or `X` for 10 in the check
/// position.
fn value(character: String, is_check: Bool) -> Maybe<Int>
case character
"X" when is_check -> Some(10)
_ -> String.to_int(character)
end
end
fn checksum(values: List<Int>) -> Int
values
|> Iter.with_index()
|> Iter.map(fn (i, v) -> v * (10 - i))
|> Iter.sum()
end
// FILE: isbn_verifier_test.nomi
import isbn_verifier
test "verifies ISBN-10 numbers"
[
("valid isbn", "3-598-21508-8", True),
("invalid isbn check digit", "3-598-21508-9", False),
("valid isbn with a check digit of 10", "3-598-21507-X", True),
("check digit is a character other than X", "3-598-21507-A", False),
("invalid check digit in isbn is not treated as zero", "4-598-21507-B", False),
("invalid character in isbn is not treated as zero", "3-598-P1581-X", False),
("X is only valid as a check digit", "3-598-2X507-9", False),
("only one check digit is allowed", "3-598-21508-96", False),
("X is not substituted by the value 10", "3-598-2X507-5", False),
("valid isbn without separating dashes", "3598215088", True),
("isbn without separating dashes and X as check digit", "359821507X", True),
("isbn without check digit and dashes", "359821507", False),
("too long isbn and no dashes", "3598215078X", False),
("too short isbn", "00", False),
("isbn without check digit", "3-598-21507", False),
("check digit of X should not be used for 0", "3-598-21515-X", False),
("empty isbn", "", False),
("input is 9 characters", "134456729", False),
("invalid characters are not ignored after checking length", "3132P34035", False),
("invalid characters are not ignored before checking length", "3598P215088", False),
("input is too long but contains a valid isbn", "98245726788", False),
]
|> subtests (description, isbn, expected), description
assert isbn_verifier.is_valid(isbn) == expected
end
endok verifies ISBN-10 numbers (21 cases)
test result: ok. 21 passed, 0 failed
How it’s written
Section titled “How it’s written”- Dashes are removed first, then each grapheme becomes a value with its position from
Iter.with_index, andMaybe.collectmakes oneMaybe<List<Int>>. A character that is neither a digit nor a check-positionXmakes the whole ISBN invalid in that one step. valuetakes the position as a named Bool,value(character, is_check: i == 9), so thecasearm reads"X" when is_check -> Some(10).- The length rule is a
whenguard on theSomearm, as in luhn, rather than a separateifbefore the conversion. The weights come from the index too,v * (10 - i). - All 21 cases are one
subteststable.