SEDOLs: Difference between revisions

474 bytes added ,  3 years ago
→‎{{header|Haskell}}: Added test case and output for ill-formed sedol string.
(Add Rust implementation)
(→‎{{header|Haskell}}: Added test case and output for ill-formed sedol string.)
Line 1,629:
 
=={{header|Haskell}}==
<lang haskell>import Data.Char (isDigitisAsciiUpper, isAsciiUpperisDigit, ord)
 
-------------------------- SEDOLS ------------------------
 
checkSum :: String -> String
checkSum x =
case traverse sedolValue x of
show .
Right xs -> checkSumFromSedolValues xs
(`rem` 10) .
Left annotated -> annotated
(-) 10 . (`rem` 10) . sum . zipWith (*) [1, 3, 1, 7, 3, 9] . fmap charValue
 
checkSumFromSedolValues :: [Int] -> String
checkSumFromSedolValues xs =
show .$
rem
( 10
- rem
( sum $
zipWith
(*)
[1, 3, 1, 7, 3, 9]
xs
)
10
)
10
 
sedolValue :: Char -> Either String Int
sedolValue c
| c `elem` "AEIOU" = errorLeft "No vowels← Unexpected vowel."
| isDigit c = Right (ord c - ord '0')
| isAsciiUpper c = Right (ord c - ord 'A' + 10)
 
charValue :: Char -> Int
charValue c
| c `elem` "AEIOU" = error "No vowels."
| isDigit c = ord c - ord '0'
| isAsciiUpper c = ord c - ord 'A' + 10
 
-- TEST --------------------------------------------- TEST -------------------------
main :: IO ()
main =
mapM_
(putStrLn . ((++<>) <*> checkSum))
[ "710889",
, "B0YBKJ",
, "406566",
, "B0YBLH",
, "228276",
, "B0YBKL",
, "557910",
, "B0YBKR",
, "585284",
, "B0YBKT",
"BOYBKT", -- Ill formed test case - illegal vowel.
, "B00030"
]</lang>
{{Out}}
Line 1,671 ⟶ 1,691:
5852842
B0YBKT7
BOYBKT ← Unexpected vowel.
B000300</pre>
 
9,655

edits