Writing raw binary to a file in Haskell?
I'm trying to do some Huffman coding in Haskell, but am having a hard time figuring out how to save the resulting 1s and 0s as they are instead of as a string. So far, I've read the following--
https://wiki.haskell.org/Dealing_with_binary_data
https://wiki.haskell.org/Binary_IO
http://hackage.haskell.org/package/binary-0.10.0.0/docs/Data-Binary.html
--but am still pretty confused as to how to deal with my specific situation. Similarly, I'm having difficulty figuring out how to write the tree structure
data HTree = Leaf Char Int
| Branch HTree HTree Int
deriving (Show)
used to decode the data to a file. (HTree is the structure I'm using to store the Huffman Tree).
haskell file-io huffman-code
add a comment |
I'm trying to do some Huffman coding in Haskell, but am having a hard time figuring out how to save the resulting 1s and 0s as they are instead of as a string. So far, I've read the following--
https://wiki.haskell.org/Dealing_with_binary_data
https://wiki.haskell.org/Binary_IO
http://hackage.haskell.org/package/binary-0.10.0.0/docs/Data-Binary.html
--but am still pretty confused as to how to deal with my specific situation. Similarly, I'm having difficulty figuring out how to write the tree structure
data HTree = Leaf Char Int
| Branch HTree HTree Int
deriving (Show)
used to decode the data to a file. (HTree is the structure I'm using to store the Huffman Tree).
haskell file-io huffman-code
2
Take a look at Data.ByteString
– typetetris
Nov 22 '18 at 6:23
See: wiki.haskell.org/Dealing_with_binary_data
– AJFarmar
Nov 22 '18 at 9:28
add a comment |
I'm trying to do some Huffman coding in Haskell, but am having a hard time figuring out how to save the resulting 1s and 0s as they are instead of as a string. So far, I've read the following--
https://wiki.haskell.org/Dealing_with_binary_data
https://wiki.haskell.org/Binary_IO
http://hackage.haskell.org/package/binary-0.10.0.0/docs/Data-Binary.html
--but am still pretty confused as to how to deal with my specific situation. Similarly, I'm having difficulty figuring out how to write the tree structure
data HTree = Leaf Char Int
| Branch HTree HTree Int
deriving (Show)
used to decode the data to a file. (HTree is the structure I'm using to store the Huffman Tree).
haskell file-io huffman-code
I'm trying to do some Huffman coding in Haskell, but am having a hard time figuring out how to save the resulting 1s and 0s as they are instead of as a string. So far, I've read the following--
https://wiki.haskell.org/Dealing_with_binary_data
https://wiki.haskell.org/Binary_IO
http://hackage.haskell.org/package/binary-0.10.0.0/docs/Data-Binary.html
--but am still pretty confused as to how to deal with my specific situation. Similarly, I'm having difficulty figuring out how to write the tree structure
data HTree = Leaf Char Int
| Branch HTree HTree Int
deriving (Show)
used to decode the data to a file. (HTree is the structure I'm using to store the Huffman Tree).
haskell file-io huffman-code
haskell file-io huffman-code
edited Nov 22 '18 at 7:16
Cameron Toy
asked Nov 22 '18 at 5:25
Cameron ToyCameron Toy
11
11
2
Take a look at Data.ByteString
– typetetris
Nov 22 '18 at 6:23
See: wiki.haskell.org/Dealing_with_binary_data
– AJFarmar
Nov 22 '18 at 9:28
add a comment |
2
Take a look at Data.ByteString
– typetetris
Nov 22 '18 at 6:23
See: wiki.haskell.org/Dealing_with_binary_data
– AJFarmar
Nov 22 '18 at 9:28
2
2
Take a look at Data.ByteString
– typetetris
Nov 22 '18 at 6:23
Take a look at Data.ByteString
– typetetris
Nov 22 '18 at 6:23
See: wiki.haskell.org/Dealing_with_binary_data
– AJFarmar
Nov 22 '18 at 9:28
See: wiki.haskell.org/Dealing_with_binary_data
– AJFarmar
Nov 22 '18 at 9:28
add a comment |
1 Answer
1
active
oldest
votes
You probably want to take a look at the Put
monad in Data.Binary.Put
. You didn't provide any code example but something to get you started:
-- just an example with some made up encoding
huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r
You would then "run" your encoder thus: runPut $ huffmanEncode myTree
obviously this is some made up encoding.. I have no clue how you want to encode this ;-)
– Erick Gonzalez
Nov 22 '18 at 12:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424380%2fwriting-raw-binary-to-a-file-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You probably want to take a look at the Put
monad in Data.Binary.Put
. You didn't provide any code example but something to get you started:
-- just an example with some made up encoding
huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r
You would then "run" your encoder thus: runPut $ huffmanEncode myTree
obviously this is some made up encoding.. I have no clue how you want to encode this ;-)
– Erick Gonzalez
Nov 22 '18 at 12:53
add a comment |
You probably want to take a look at the Put
monad in Data.Binary.Put
. You didn't provide any code example but something to get you started:
-- just an example with some made up encoding
huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r
You would then "run" your encoder thus: runPut $ huffmanEncode myTree
obviously this is some made up encoding.. I have no clue how you want to encode this ;-)
– Erick Gonzalez
Nov 22 '18 at 12:53
add a comment |
You probably want to take a look at the Put
monad in Data.Binary.Put
. You didn't provide any code example but something to get you started:
-- just an example with some made up encoding
huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r
You would then "run" your encoder thus: runPut $ huffmanEncode myTree
You probably want to take a look at the Put
monad in Data.Binary.Put
. You didn't provide any code example but something to get you started:
-- just an example with some made up encoding
huffmanEncode :: HTree -> Put
huffmanEncode (Leaf c val) = do
putWord16be 0xf00
putCharUtf8 c
putWord32be $ fromIntegral val
huffmanEncode (Branch l r x) = do
putWord16be 0xbaf
putWord32be $ fromIntegral x
huffmanEncode l
huffmanEncode r
You would then "run" your encoder thus: runPut $ huffmanEncode myTree
answered Nov 22 '18 at 12:48
Erick GonzalezErick Gonzalez
1544
1544
obviously this is some made up encoding.. I have no clue how you want to encode this ;-)
– Erick Gonzalez
Nov 22 '18 at 12:53
add a comment |
obviously this is some made up encoding.. I have no clue how you want to encode this ;-)
– Erick Gonzalez
Nov 22 '18 at 12:53
obviously this is some made up encoding.. I have no clue how you want to encode this ;-)
– Erick Gonzalez
Nov 22 '18 at 12:53
obviously this is some made up encoding.. I have no clue how you want to encode this ;-)
– Erick Gonzalez
Nov 22 '18 at 12:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424380%2fwriting-raw-binary-to-a-file-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Take a look at Data.ByteString
– typetetris
Nov 22 '18 at 6:23
See: wiki.haskell.org/Dealing_with_binary_data
– AJFarmar
Nov 22 '18 at 9:28