Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading in a binary file in haskell

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

readBinaryFile :: Filename -> IO Data.ByteString

I've got the functional 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?

like image 582
Clark Gaebel Avatar asked Aug 09 '10 06:08

Clark Gaebel


1 Answers

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
like image 85
BT. Avatar answered Sep 22 '22 10:09

BT.