0

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: "'
1
  • 2
    A note on terminology. print isn't "throwing [an] error", that would imply you are executing the program. What is happening is the compiler is emitting a type checking error regarding the print statement. Commented Sep 29, 2016 at 6:07

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

If I store input!!0 into a variable first and then use it in the putStrLn, I get "Couldn't match type Word8' with IO a0'" error.
Doing this databasetype <- input!!0 to assign the variable and then Prelude.putStrLn $ "DBType: " ++ show (databasetype) to print out. Getting the "Couldn't match type Word8' with IO a0'" error.
@Vlam Ah. So you will want to have let databasetype = input!!0 for the assignment. <- actually is part of do-notation, which is something different. I recommend you read up on do-notation a bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.