我的打印語句一直拋出錯誤,不太明白髮生了什麼。如何在Haskell中打印出二進制文件中的字節?
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]
獲取以下錯誤:
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: "'
關於術語的說明。 'print'不是「拋出[錯誤]」,這意味着你正在執行程序。發生了什麼事是編譯器發出關於print語句的類型檢查錯誤。 –