简体   繁体   中英

ByteStrings in Haskell

So i am trying to write a program that can read in a java class file as bytecode. For this i am using Data.Binary and Data.ByteStream. The problem i am having is because im pretty new to Haskell i am having trouble actually using these tools.

module Main where
import Data.Binary.Get
import Data.Word
import qualified Data.ByteString.Lazy as S

getBinary :: Get Word8
getBinary = do
a <- getWord8
return (a)

main :: IO ()
main = do
contents <- S.getContents
print (getBinary contents)

This is what i have come up with so far and i fear that its not really even on the right track. Although i know this question is very general i would appreciate some help with what i should be doing with the reading.

Can you use one of the existing Java analyis/parsing tools in Haskell? Eg

http://hackage.haskell.org/package/jarfind

If you need to learn how to use Data.Binary, I suggest Real World Haskell: http://book.realworldhaskell.org/read/code-case-study-parsing-a-binary-data-format.html

This is actually one of the worst applications in which to use Haskell. Why?

Lots of I/O means that you need to deal with monads; which I would suggest tackling once you become comfortable with the other unique features of the language, not before. They're a complex topic even for those with graduate degrees in mathematics (or so I hear). Not only that, if you start out writing code that's mostly I/O, you may get the impression that you can and should do a lot of algorithms imperatively in Haskell. This is not the case. Perhaps most importantly for you, I'm guessing that you were attracted to this language because of it's almost notoriously short and straightforward chunks of code. This is the case for nearly everything in the language besides I/O and manual memory management (which is I/O, really).

I would suggest writing your program in C, which is perfectly suited for this task, and have your first Haskell programs be things that you would consider rather tricky in other languages. I'm a particular fan of machine-learning algorithms, myself, but whatever data structures or algorithms you seemed to find difficult in other languages, try to rework for Haskell.

Just get used to writing a lot less code. My first major Haskell app was a nueral-network training library that used both reinforcement learning and genetic algorithms, multithreaded. In 350 lines of code (including generous amounts of commenting). That's the serious power of Haskell, in my opinion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM