views:

140

answers:

2

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.

A: 

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.

Lion Kabob
I would love to do that, unfortunately this is an assignment for a class which requires it to be in Haskell.
Jon
Ah, well that changes things. In that case, the general organization is to have as little monadic code as possible. I've heard it described as "a thin shell of imperative mold around a huge delicious blob of functional magicalness". In other words, try to have as little imperative code as possible, and have that code call lots of pure functions with the retrieved data.
Lion Kabob
Lion: C doesn't have parser combinators for parsing binary files the way Haskell does. This isn't an IO problem, its a parsing problem - something Haskell's particular good at.
Don Stewart
Just because it reads in a java class file, doesn't mean there's lots of I/O.
Wei Hu
Understanding Category Theory monads in full context is a complex topic. Monads in Haskell are merely named for the former and are not complex at all. It's a type class with two functions, and the standard library code for most of the common instances is barely longer than the code in the question above. Just ignore all the monad tutorials, most are useless or worse.
camccann
+2  A: 

Can you use one of the existing Java analyis/parsing tools in Haskell? E.g.

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

Don Stewart