IBAN: Difference between revisions

581 bytes added ,  10 years ago
Haskell
(Added Haskell)
(Haskell)
Line 905:
 
=={{header|Haskell}}==
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>
validateIBAN :: String -> Either String String
validateIBAN [] = Left "No IBAN number."
validateIBAN xs =
Line 950 ⟶ 952:
in ys : convertLetters xs
-- see if the number is valid
check = if p4 `mod` 97 == 1sane
then Rightif xsp4 `mod` 97 == 1
else invalidBecause "Validation failed." then Right xs
else invalidBecause "Validation failed."
else invalidBecause "Number contains illegal digits."
 
invalidBecause reason = Left $ "Invalid IBAN number " ++ xs ++
Line 970 ⟶ 974:
validateIBAN "GB82 WEST 1234 5698 7654 3"
Left "Invalid IBAN number GB82 WEST 1234 5698 7654 3: Number length does not match."
 
validateIBAN "GB82 WEsT 1234 5698 7654 32"
Left "Invalid IBAN number GB82 WEsT 1234 5698 7654 32: Number contains illegal digits."
</pre>
 
=={{header|J}}==
<lang J>
Anonymous user