import Data.Char
blockCode :: S
lett2num :: Char -> Int
lett2num y
| (or
num2bin :: Int -> [Int]
num2bin n: negative number"
where n2b 0 = []
n2b n = n `mod` 2 : n2b (n `div` 2)
答案 0 :(得分:1)
您可以使用concatMap show
将列表转换为字符串:
Main> num2bin 8
[0,0,0,1]
Main> concatMap show $ num2bin 8
"0001"
但请注意您的功能输出已反转。
要一次完成所有事情,请执行
num2bin :: Int -> String
num2bin n
| n >= 0 = concatMap show $ reverse $ n2b n
| otherwise = error "num2bin: negative number"
where n2b 0 = []
n2b n = n `mod` 2 : n2b (n `div` 2)
答案 1 :(得分:0)
函数将整数转换为二进制:
num2bin :: (Integral a, Show a) => a -> String
num2bin 0 = "0"
num2bin 1 = "1"
num2bin n
| n < 0 = error "Negative number"
| otherwise = num2bin (n `div` 2) ++ show (n `mod` 2)