开发者

Reading in a binary file in haskell

开发者 https://www.devze.com 2023-01-10 14:30 出处:网络
How could I write a function with a definition something like... readBinaryFile :: Filename -> IO Data.ByteString

How could I write a function with a definition something like...

readBinaryFile :: Filename -> IO Data.ByteString

I've got the functio开发者_如何学编程nal parts of Haskell down, but the type system and monads still make my head hurt. Can someone write and explain how that function works to me?


import Data.ByteString.Lazy
readFile fp

easy as pie man. Knock off the lazy if you don't want the string to be lazy.

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


readBinaryFile :: Filename -> IO Data.ByteString

This is simply the Data.ByteString.readFile function, which you should never have to write, since it is in the bytestring package.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号