Vlam
Vlam

Reputation: 1798

How to print out bytes from binary file in Haskell?

My print statement keeps throwing error, don't really understand what is going on.

import Data.ByteString.Lazy as BS
import Data.Word
import Data.Bits

readByte :: String -> IO [Word8]
readByte fp = do
    contents <- BS.readFile fp
    return $ Prelude.take 5 $ unpack contents

main :: IO ()
main = do
    input <- readByte "DATA.BIN"
    print "Byte 0: " ++ [input!!0]

Getting the below error:

Couldn't match expected type `[()]' with actual type `IO ()'
In the return type of a call of `print'
In the first argument of `(++)', namely `print "Byte 0: "'

Upvotes: 0

Views: 535

Answers (1)

Alec
Alec

Reputation: 32319

Haskell is parsing print "Byte 0: " ++ [input!!0] as (print "Byte 0: ") ++ [input!!0], which is probably not what you intended. You might want

main :: IO ()
main = do
    input <- readByte "DATA.BIN"
    putStrLn $ "Byte 0: " ++ show (input!!0)

instead

Upvotes: 2

Related Questions