【问题标题】:Reading in a binary file in haskell在haskell中读取二进制文件
【发布时间】:2010-08-09 06:06:49
【问题描述】:

我怎样才能编写一个定义类似于...的函数

readBinaryFile :: Filename -> IO Data.ByteString

我已经搞定了 Haskell 的功能部分,但是类型系统和 monad 仍然让我头疼。有人可以向我解释一下该功能是如何工作的吗?

【问题讨论】:

    标签: haskell binary bytestring


    【解决方案1】:
    import Data.ByteString.Lazy
    readFile fp
    

    像馅饼一样容易。如果您不想让字符串变得懒惰,请关闭懒惰。

    import Data.ByteString.Lazy as BS
    import Data.Word
    import Data.Bits
    
    fileToWordList :: String -> IO [Word8]
    fileToWordList fp = do
        contents <- BS.readFile fp
        return $ unpack contents
    

    【讨论】:

      【解决方案2】:

      readBinaryFile :: 文件名 -> IO Data.ByteString

      这只是 Data.ByteString.readFile 函数,您永远不必编写它,因为它位于 bytestring 包中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-26
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多