我想创建IBAN,但第一步是创建BBAN。前导零添加到数字中。这是代码:
iban :: [Char] -> [Char] -> [Char] -> [Char]
iban a b c | ((length a == 4) && (length b <= 6) && (length c <= 10)) = createBBAN a b c
| otherwise = "Error"
createBBAN x y z | ((length y) < 6) = createBBAN x ("0" ++ y) z
| ((length z) < 10) = createBBAN x y ("0" ++ z)
| otherwise = x ++ y ++ z
但我想让iban这样:
iban :: Integer -> Integer -> Integer -> String
我该怎么做?
答案 0 :(得分:2)
iban :: Integer -> Integer -> Integer -> String
iban a b c | ((a < 10000) && (a>8999) && (b < 1000000) && (c < 10000000000)) = createBBAN (show a) (show b) (show c)
| otherwise = "Error"
createBBAN x y z | ((length y) < 6) = createBBAN x ("0" ++ y) z
| ((length z) < 10) = createBBAN x y ("0" ++ z)
| otherwise = x ++ y ++ z
当然你可以稍后在createBBAN中应用show,但我认为这没有多大意义,因为那时你需要多次转换。
=&gt;表示Integer by String via
显示
答案 1 :(得分:1)
iban :: Integer -> Integer -> Integer -> String
iban a b c | and [(length $ show a) == 4,
(length $ show b) == 6,
(length $ show c) <= 10] = createBBAN a b c
| otherwise = "Error"
createBBAN x y z | ((length y) < 6) = createBBAN x ("0" ++ y) z
| ((length z) < 10) = createBBAN x y ("0" ++ z)
| otherwise = x ++ y ++ z
Typecast to string并获取长度。您也可以调用log base 10来提取位数。
脑编译,希望没问题。