【发布时间】:2016-10-05 05:20:00
【问题描述】:
我在下面有一个名为 test2.hs 的文件,我试图通过二进制文件中的偏移量读取一个字节(Word8)并将其用作 Int。下面的例子是试图读取二进制文件中的第二个字节。
import qualified Data.ByteString.Lazy as BS
import Data.Word
import Data.Bits
import Data.Binary.Get
getuint8 = do
uint8 <- getWord8
return uint8
readuint8 :: BS.ByteString -> Int -> Int
readuint8 contents startpos = do
return $ runGet getuint8 (drop startpos contents)
main :: IO ()
main = do
let myfile = "DATA.BIN"
contents <- BS.readFile myfile
let stuff = readuint8 contents 1
print stuff
不太清楚为什么会出现以下错误:
test2.hs:12:9: error:
• Couldn't match expected type ‘Int’ with actual type ‘m0 Word8’
• In a stmt of a 'do' block:
return $ runGet getuint8 (drop startpos contents)
In the expression:
do { return $ runGet getuint8 (drop startpos contents) }
In an equation for ‘readuint8’:
readuint8 contents startpos
= do { return $ runGet getuint8 (drop startpos contents) }
test2.hs:12:35: error:
• Couldn't match expected type ‘BS.ByteString’
with actual type ‘[a0]’
• In the second argument of ‘runGet’, namely
‘(drop startpos contents)’
In the second argument of ‘($)’, namely
‘runGet getuint8 (drop startpos contents)’
In a stmt of a 'do' block:
return $ runGet getuint8 (drop startpos contents)
test2.hs:12:49: error:
• Couldn't match expected type ‘[a0]’
with actual type ‘BS.ByteString’
• In the second argument of ‘drop’, namely ‘contents’
In the second argument of ‘runGet’, namely
‘(drop startpos contents)’
In the second argument of ‘($)’, namely
‘runGet getuint8 (drop startpos contents)’
【问题讨论】: