András Kovács
András Kovács

Reputation: 30113

Reading from files to arrays in Haskell

I have a trie-based dictionary on my drive that is encoded as a contiguous array of bit-packed 4-byte trie nodes. In Python I would read it to an actual array of 4-byte integers the following way:

import array
trie = array.array('I')
try:
    trie.fromfile(open("trie.dat", "rb"), some_limit)
except EOFError:
    pass

How can I do the same in Haskell (reading from a file to an Array or Vector)? The best I could come up with is to read the file as usual and then take the bytes in chunks of four and massage them together arithmetically, but that's horribly ugly and also introduces a dependency on endianness.

Upvotes: 2

Views: 519

Answers (1)

Don Stewart
Don Stewart

Reputation: 137937

encoded as a contiguous array of bit-packed 4-byte trie nodes

I presume the 'encoding' here is some Python format? You say "raw C-style array"?

To load the data of this binary (or any other format) into Haskell you can use the Data.Binary library, and provide an instance of Binary for your custom format.

For many existing data interchange formats there are libraries on Hackage, however you would need to specify the format. For e.g. image data, there is repa-devil.

For truly raw data, you can mmap it to a bytestring, then process it further into a data structure.

Upvotes: 3

Related Questions