ISBN13 check digit: Difference between revisions
m
→{{header|Haskell}}: Applied Ormolu, used an applicative.
m (→{{header|Haskell}}: Tidied variant expressed in terms of cycle.) |
m (→{{header|Haskell}}: Applied Ormolu, used an applicative.) |
||
Line 1,137:
=={{header|Haskell}}==
<lang haskell>import
import
import Text.Printf (printf)
testISBNs :: [String]▼
pair :: Num a => [a] -> [(a, a)]
pair [] = []
pair xs = p (take 2 xs) : pair (drop 2 xs)
where
where p ps = case ps of (x:y:zs) -> (x,y)▼
p ps = case ps of
(x:zs) -> (x,0)▼
validIsbn13 :: String -> Bool
validIsbn13 isbn
| length (digits isbn) /= 13 = False
| otherwise = calc isbn `rem` 10 == 0
where
calc = sum . map (\(x, y) -> x + y * 3) . pair . digits
main :: IO ()
main =
main = forM_ testISBNs (\isbn -> printf "%s: Valid: %s\n" isbn (show $ validIsbn13 isbn))</lang>▼
forM_
testISBNs
▲
▲testISBNs :: [String]
testISBNs =
[ "978-1734314502",
"978-1734314509",
"978-1788399081",
"978-1788399083"
]</lang>
{{out}}
<pre>978-1734314502: Valid: True
|