简体   繁体   English

Haskell中的字节串

[英]ByteStrings in Haskell

So i am trying to write a program that can read in a java class file as bytecode. 因此,我试图编写一个程序,该程序可以将Java类文件中的字节码读取。 For this i am using Data.Binary and Data.ByteStream. 为此,我正在使用Data.Binary和Data.ByteStream。 The problem i am having is because im pretty new to Haskell i am having trouble actually using these tools. 我遇到的问题是因为对Haskell而言,这是我的新手,我在实际使用这些工具时遇到了麻烦。

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? 您可以在Haskell中使用现有的Java分析/解析工具之一吗? Eg 例如

http://hackage.haskell.org/package/jarfind 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 如果您需要学习如何使用Data.Binary,建议您使用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. 实际上,这是使用Haskell的最糟糕的应用程序之一。 Why? 为什么?

Lots of I/O means that you need to deal with monads; 很多I / O意味着您需要处理monad。 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. 不仅如此,如果您开始编写主要是I / O的代码,您可能会感到,您可以并且应该在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 / O和手动内存管理(实际上是I / O)之外,几乎所有语言的情况都是如此。

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. 我建议您使用C语言编写程序,该程序非常适合此任务,并且让您的第一个Haskell程序成为您认为用其他语言比较棘手的东西。 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. 我本人是机器学习算法的特别拥护者,但是,如果您发现其他语言中的数据结构或算法似乎很困难,请尝试为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. 我的第一个主要Haskell应用程序是一个神经网络训练库,该库同时使用了强化学习和遗传算法(多线程)。 In 350 lines of code (including generous amounts of commenting). 350行代码(包括大量注释)。 That's the serious power of Haskell, in my opinion. 我认为这就是Haskell的强大功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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