私のprintステートメントはスローエラーを起こしていますが、何が起こっているのかは分かりません。Haskellでバイナリファイルからバイトをプリントアウトするには?
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ステートメントはスローエラーを起こしていますが、何が起こっているのかは分かりません。Haskellでバイナリファイルからバイトをプリントアウトするには?
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: "'
Haskellは(print "Byte 0: ") ++ [input!!0]
としてprint "Byte 0: " ++ [input!!0]
を解析している、あなたが意図したもので、おそらくではありません。
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]
以下のエラーを取得します。代わりに
最初に変数!input!0を格納してからputStrLnで使用すると、「Word8」と「IO a0」というエラーが発生しました。 – Vlam
このdatabasetype < - input !! 0を使用して変数を割り当て、次にPrelude.putStrLn $ "DBType:" ++ show(databasetype)を使用して出力します。 "「Word8」と「IO a0」のエラーが発生しました。 – Vlam
@Vlam Ah。だから、あなたは 'let databasetype = input !! 0'を代入にしたいでしょう。 '< - 'は実際にdo-notationの一部ですが、これは異なるものです。私はあなたが少しの表記を読むことをお勧めします。 – Alec
用語上の注意事項をお勧めします。 'print'は「エラーを投げる」という意味ではありません。プログラムを実行していることを意味します。起こっていることは、コンパイラがprint文に関する型チェックエラーを出していることです。 –