Thursday, April 28, 2011

Haskell libraries you should use

Since Hackage doesn't help you separate the good libraries from the bad, I wrote down a list of libraries I consider good. I hope this list can serve as a starting point for new Haskell programmers who are lost in the Hackage jungle. The list isn't exhaustive and I intend to flesh it out in the future.

I won't provide a separate motivation for each library in the list. The libraries I've listed have a sensible API, are well document, and well tested. In addition, by using these libraries your Haskell code is likely to perform well without you having to think too much about performance.

Haskell libraries you should use

bytestring: Defines the ByteString type. Use bytestring when you need to represent binary data, such as network packets.

text: Defines the Text type. Use the text package if you need to work with Unicode text data. The text package deals with lots of tricky internationalization issues for you, like how to lowercase/uppercase strings correctly in different languages. The package includes functions that decode/encode Text values from and to ByteString values.

vector: Defines types for immutable and mutable vectors. Use this package whenever you'd use an array in another language. Since the built-in list type is so convenient to use, programmers sometimes use it when it's not appropriate, which makes their programs perform badly. Remember, the built-in list type is implemented as a linked list, which the associated memory bloat and cache issues.

aeson: Lets you work with JSON data easily and efficiently.

binary: Serialization and deserialization of binary data. Use this package when you need to parse some predefined binary protocol. The cereal package is a popular alternative.

attoparsec: Good for parsing protocols that mix binary and ASCII data, like HTTP.

10 comments:

  1. does the `vector` package provide unboxed vectors as well? Say I need to store 10 mio. `Int`s, should I still use `vector`?

    ReplyDelete
  2. @Anon yes. Just take a look for yourself:

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

    ReplyDelete
  3. Anonymous,

    You want to use Data.Vector.Unboxed. For an N element vector it will use N*4 (or N*8) bytes of memory, depending on your machine's architecture.

    ReplyDelete
  4. John, so vector can compress _any_ data down to 4 or 8 bytes? Impressive! :-)

    ReplyDelete
  5. This looks rather similar to the Haskell Platform; perhaps it would be useful to suggest the ones that aren't included yet for inclusion?

    ReplyDelete
  6. The 4 or 8 byte per element number was for Ints, which the first commenter asked about.

    ReplyDelete
  7. @Johan
    Hey, Johan! Can I translate your article to russian language?

    ReplyDelete
  8. kreed,

    Sure. I'd appreciate if you'd link to the original article and give me credit.

    ReplyDelete
  9. Johan, here is article http://kreed131.blogspot.com/2011/08/haskell.html.

    ReplyDelete