RosettaCodeData/Task/SEDOLs/Haskell/sedols.hs

32 lines
660 B
Haskell
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
import Data.Char (isDigit, isAsciiUpper, ord)
2013-04-10 23:57:08 -07:00
2017-09-23 10:01:46 +02:00
checkSum :: String -> String
checkSum =
show .
(`rem` 10) .
2019-09-12 10:33:56 -07:00
(-) 10 . (`rem` 10) . sum . zipWith (*) [1, 3, 1, 7, 3, 9] . fmap charValue
2013-04-10 23:57:08 -07:00
2017-09-23 10:01:46 +02:00
charValue :: Char -> Int
charValue c
| c `elem` "AEIOU" = error "No vowels."
| isDigit c = ord c - ord '0'
| isAsciiUpper c = ord c - ord 'A' + 10
2013-04-10 23:57:08 -07:00
2017-09-23 10:01:46 +02:00
-- TEST ----------------------------------------------------------------------
main :: IO ()
main =
mapM_
(putStrLn . ((++) <*> checkSum))
[ "710889"
, "B0YBKJ"
, "406566"
, "B0YBLH"
, "228276"
, "B0YBKL"
, "557910"
, "B0YBKR"
, "585284"
, "B0YBKT"
, "B00030"
]