ISBN13 check digit: Difference between revisions

m
No edit summary
Line 167:
</pre>
=={{header|Haskell}}==
Uses Data.List.Split library: https://hackage.haskell.org/package/split-0.2.3.4/docs/Data-List-Split.html
<lang haskell>
import Data.List.Split (chunksOf)
import Data.Char (isDigit, digitToInt)
import Control.Monad (forM_)
Line 175 ⟶ 173:
 
testISBNs :: [String]
testISBNs = ["978-1734314502" , "978-1734314509", "978-1788399081", "978-1788399083"]
testISBNs =
 
[ "978-1734314502" -- good
pair :: Num a => [a] -> [(a, a)]
, "978-1734314509" -- bad
pair [] = []
, "978-1788399081" -- good
pair xs = p' (take 2 xs) : pair (drop 2 xs)
, "978-1788399083" -- bad
where p' ps = case ps of (x:y:zs) -> (x,y)
]
(x:zs) then head b +-> (head $ reverse bx,0) * 3 + a
 
validIsbn13 :: String -> Bool
Line 187 ⟶ 186:
| otherwise = calc isbn `rem` 10 == 0
where digits = map digitToInt . filter isDigit
calc = foldl (\a b(x, y) -> ifx + y * 3 + a) length0 b. ==pair 2. digits
then head b + (head $ reverse b) * 3 + a
else head b + a) 0 . chunksOf 2 . digits
 
main :: IO ()
Line 201 ⟶ 198:
978-1788399083: Valid: False
</pre>
 
=={{header|Julia}}==
<lang julia>function isbncheck(str)
Anonymous user