IBAN: Difference between revisions

Content added Content deleted
(Haskell)
(Haskell)
Line 907: 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).
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>
<lang Haskell>
import Data.Char (toUpper)

validateIBAN :: String -> Either String String
validateIBAN :: String -> Either String String
validateIBAN [] = Left "No IBAN number."
validateIBAN [] = Left "No IBAN number."
Line 916: Line 918:
else check
else check
where
where
-- remove blanks
-- remove blanks and make all letters uppercase
normalized = concat $ words xs
normalized = map toUpper $ concat $ words xs
-- get the country code
-- get the country code
country = take 2 normalized
country = take 2 normalized
Line 965: Line 967:
validateIBAN "GB82 WEST 1234 5698 7654 32"
validateIBAN "GB82 WEST 1234 5698 7654 32"
Right "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"
validateIBAN "GB82 WEST 1234 5698 7654 31"
Line 975: Line 980:
Left "Invalid IBAN number GB82 WEST 1234 5698 7654 3: Number length does not match."
Left "Invalid IBAN number GB82 WEST 1234 5698 7654 3: Number length does not match."


validateIBAN "GB82 WEsT 1234 5698 7654 32"
validateIBAN "GB82 _EST 1234 5698 7654 32"
Left "Invalid IBAN number GB82 WEsT 1234 5698 7654 32: Number contains illegal digits."
Left "Invalid IBAN number GB82 _EST 1234 5698 7654 32: Number contains illegal digits."
</pre>
</pre>