Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you save a tree data structure to binary file in Haskell
    text
    copied!<p>I'm trying to save a simple (but quite big) Tree structure into a binary file using Haskell. The structure looks something like this:<pre> -- For simplicity assume each Node has only 4 childs data Tree = Node [Tree] | Leaf [Int] </pre> And here is how I need the data look on disk:</p> <ol> <li>Each node starts with four 32-bit offsets to it's children, then follow the childs.</li> <li>I don't care much about the leafs, let's say it's just n consecutive 32-bit numbers.</li> <li>For practival purposes I would need some node labels or some other additional data but right now I don't care about that much neither.</li> </ol> <p>It apears to me that Haskellers first choice when writing binary files is the Data.Binary.Put library. But with that I have a problem in the bullet #1. In particular, when I'm about to write a Node to a file, to write down the child offsets I need to know my current offset and the size of each child.</p> <p>This is not something that Data.Binary.Put provides so I thought this must be a perfect application of Monad transformers. But even though it sounds cool and functional, so far I have not been successfull with this approach.</p> <p>I asked two other questions that I thought would help me solve the problem <a href="https://stackoverflow.com/questions/4828902/why-wrapping-the-data-binary-put-monad-creates-a-memory-leak">here</a> and <a href="https://stackoverflow.com/questions/5019655/why-wrapping-the-data-binary-put-monad-creates-a-memory-leak-part-2">here</a>. I must say that each time I received very nice answers that helped me progress further but unfortunatelly I am still unable to solve the problem as a whole.</p> <p><a href="http://hpaste.org/44419/how_do_you_save_a_tree_data_st" rel="nofollow noreferrer">Here</a> is what I've got so far, it still leaks too much memory to be practical.</p> <p>I would love to have solution that uses such functional approach, but would be grateful for any other solution as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload