IBAN: Difference between revisions

153 bytes added ,  10 years ago
Haskell
(Haskell)
(Haskell)
Line 907:
This program uses the Maybe and Either monads to handle failures. Values of type 'Maybe a' can contain 'Nothing' (no value) or 'Just a' (a value of type 'a'). Values of type 'Either a b' contain 'Left b' (usually indicating failure) or 'Right c' (usually indicating success).
<lang Haskell>
import Data.Char (toUpper)
 
validateIBAN :: String -> Either String String
validateIBAN [] = Left "No IBAN number."
Line 916 ⟶ 918:
else check
where
-- remove blanks and make all letters uppercase
normalized = map toUpper $ concat $ words xs
-- get the country code
country = take 2 normalized
Line 965 ⟶ 967:
validateIBAN "GB82 WEST 1234 5698 7654 32"
Right "GB82 WEST 1234 5698 7654 32"
 
validateIBAN "gb82 West 1234 5698 7654 32"
Right "gb82 West 1234 5698 7654 32"
 
validateIBAN "GB82 WEST 1234 5698 7654 31"
Line 975 ⟶ 980:
Left "Invalid IBAN number GB82 WEST 1234 5698 7654 3: Number length does not match."
 
validateIBAN "GB82 WEsT_EST 1234 5698 7654 32"
Left "Invalid IBAN number GB82 WEsT_EST 1234 5698 7654 32: Number contains illegal digits."
</pre>
 
Anonymous user