ISBN13 check digit: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 167: | Line 167: | ||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Uses Data.List.Split library: https://hackage.haskell.org/package/split-0.2.3.4/docs/Data-List-Split.html |
|||
<lang haskell> |
<lang haskell> |
||
import Data.List.Split (chunksOf) |
|||
import Data.Char (isDigit, digitToInt) |
import Data.Char (isDigit, digitToInt) |
||
import Control.Monad (forM_) |
import Control.Monad (forM_) |
||
Line 175: | Line 173: | ||
testISBNs :: [String] |
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) |
|||
] |
|||
⚫ | |||
validIsbn13 :: String -> Bool |
validIsbn13 :: String -> Bool |
||
Line 187: | Line 186: | ||
| otherwise = calc isbn `rem` 10 == 0 |
| otherwise = calc isbn `rem` 10 == 0 |
||
where digits = map digitToInt . filter isDigit |
where digits = map digitToInt . filter isDigit |
||
calc = foldl (\a |
calc = foldl (\a (x, y) -> x + y * 3 + a) 0 . pair . digits |
||
⚫ | |||
else head b + a) 0 . chunksOf 2 . digits |
|||
main :: IO () |
main :: IO () |
||
Line 201: | Line 198: | ||
978-1788399083: Valid: False |
978-1788399083: Valid: False |
||
</pre> |
</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<lang julia>function isbncheck(str) |
<lang julia>function isbncheck(str) |