SEDOLs: Difference between revisions
→{{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 (
-------------------------- SEDOLS ------------------------
checkSum :: String -> String
checkSum x =
case traverse sedolValue x of
show .▼
Right xs -> checkSumFromSedolValues xs
Left annotated -> annotated
checkSumFromSedolValues :: [Int] -> String
checkSumFromSedolValues xs =
rem
( 10
- rem
( sum $
zipWith
(*)
[1, 3, 1, 7, 3, 9]
xs
)
10
)
10
sedolValue :: Char -> Either String Int
sedolValue c
| isDigit c = Right (ord c - ord '0')▼
| isAsciiUpper c = Right (ord c - ord 'A' + 10)▼
▲ | c `elem` "AEIOU" = error "No vowels."
▲ | isDigit c = ord c - ord '0'
▲ | isAsciiUpper c = ord c - ord 'A' + 10
main :: IO ()
main =
mapM_
(putStrLn . ((
[ "710889",
"BOYBKT", -- Ill formed test case - illegal vowel.
]</lang>
{{Out}}
Line 1,671 ⟶ 1,691:
5852842
B0YBKT7
BOYBKT ← Unexpected vowel.
B000300</pre>
|