Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/ISBN13-check-digit/Haskell/isbn13-check-digit-1.hs
Normal file
28
Task/ISBN13-check-digit/Haskell/isbn13-check-digit-1.hs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import Data.Char (digitToInt, isDigit)
|
||||
import Text.Printf (printf)
|
||||
|
||||
pair :: Num a => [a] -> [(a, a)]
|
||||
pair [] = []
|
||||
pair xs = p (take 2 xs) : pair (drop 2 xs)
|
||||
where
|
||||
p ps = case ps of
|
||||
(x : y : zs) -> (x, y)
|
||||
(x : zs) -> (x, 0)
|
||||
|
||||
validIsbn13 :: String -> Bool
|
||||
validIsbn13 isbn
|
||||
| length (digits isbn) /= 13 = False
|
||||
| otherwise = calc isbn `rem` 10 == 0
|
||||
where
|
||||
digits = map digitToInt . filter isDigit
|
||||
calc = sum . map (\(x, y) -> x + y * 3) . pair . digits
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
(printf "%s: Valid: %s\n" <*> (show . validIsbn13))
|
||||
[ "978-1734314502",
|
||||
"978-1734314509",
|
||||
"978-1788399081",
|
||||
"978-1788399083"
|
||||
]
|
||||
20
Task/ISBN13-check-digit/Haskell/isbn13-check-digit-2.hs
Normal file
20
Task/ISBN13-check-digit/Haskell/isbn13-check-digit-2.hs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import Data.Char (digitToInt, isDigit)
|
||||
|
||||
isISBN13 :: String -> Bool
|
||||
isISBN13 =
|
||||
(0 ==)
|
||||
. flip rem 10
|
||||
. sum
|
||||
. flip
|
||||
(zipWith ((*) . digitToInt) . filter isDigit)
|
||||
(cycle [1, 3])
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
(print . ((,) <*> isISBN13))
|
||||
[ "978-1734314502",
|
||||
"978-1734314509",
|
||||
"978-1788399081",
|
||||
"978-1788399083"
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue