0

我想将十六进制转换为 bin haskell,这就是我所做的,但它似乎是错误的。有人可以帮忙吗?

bin:: Char -> String
bin'0' = "0000"
bin'1' = "0001"
bin'2' = "0010"
bin'3' = "0011"
bin'4' = "0100"
bin'5' = "0101"
bin'6' = "0110"
bin'7' = "0111"
bin'8' = "1000"
bin'9' = "1001"
bin'a' = "1010"
bin 'b' = "1011"
bin'c' = "1100"
bin'd' = "1101"
bin'e' = "1110"
bin'f' = "1111"
bin j = error "Your input is not a hex digit"

hex2bin :: Show a => Handle -> [a] -> IO ()
hex2bin fh [] = return ()
hex2bin fh (x:xs) = do 
    hPutStr fh (show(x) ++ "\n")
    hex2bin fh xs
4

1 回答 1

1

在你的hex2bin你永远不会打电话bin。你在元素上调用它,所以:

hex2bin :: Handle -> String -> IO ()
hex2bin fh [] = return ()
hex2bin fh (x:xs) = do 
    hPutStrLn fh (bin x)
    hex2bin fh xs

您可以使用mapM_ :: (Monad m, Foldable f) => (a -> m b) -> f a -> m ()

hex2bin :: Handle -> String -> IO ()
hex2bin fh = mapM_ (hPutStrLn fh . bin)
于 2021-03-30T21:09:04.223 回答