2014-03-30 62 views
0

我想创建IBAN,但第一步是创建BBAN。前导零被添加到数字中。下面是代码:将IBAN字符串的类型更改为字符串

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 

我怎样才能做到这一点?

回答

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 

类型强制转换为字符串,并得到长度。你也可以直接调用log base 10来提取数字的位数。

大脑编译,希望没问题。

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但我认为这不会使该多大意义,因为那么你就需要多次转换。
=>经由

代表整数由字符串显示