Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6077397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:42:58+00:00 2026-05-23T10:42:58+00:00

I started to play around with haskell and I want to write a parser

  • 0

I started to play around with haskell and I want to write a parser for binary data that I already did with all programming languages I tried so far.

But I am struggling with the monad concept and a general handling of data in haskell.
So far I have the following:

data TestData = T { value :: String } deriving (Show)
data TestData2 = T2 { value2 :: String } deriving (Show)

testFunc :: Handle -> IO BS.ByteString
testFunc hs = BS.hGet hs 4


main = do
            handle <- SIO.openFile "c:/temp/test.bin" SIO.ReadMode
            contents1 <- BS.hGet handle 4
            contents2 <- testFunc handle
            contents3 <- BS.hGet handle 4
            putStrLn (show (T (bytesToString contents1)))
            putStrLn (show (T (bytesToString contents2)))
            putStrLn (show (T (bytesToString contents3)))

The data I want to read are multiple nested structs(spoken i C).
So what I want to know is how to write my testFunc that it will return a data type of my own TestData2 type. But still can be used in the do chain of functions.
Anyone can give me a hint?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:42:58+00:00Added an answer on May 23, 2026 at 10:42 am

    You can do it this way using do notation:

    testFunc :: Handle -> IO TestData2
    testFunc hs = do contents <- BS.hGet hs 4
                     return $ T2 (bytesToString contents)
    

    Or you can do it this way using <$> from Control.Applicative:

    testFunc :: Handle -> IO TestData2
    testFunc hs = T2 . bytesToString <$> BS.hGet hs 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, I've started to play around with Fluent NHibernate and now I want
I've started to play around with PowerShell and am trying to get it to
I've just started to play with CodeIgniter 1.7.2 and imendialtly noticed that there is
I have started to play around with boost python a bit and ran into
Started to play around with Jenkins. Created new job and entered SVN repository URL.
I started to play around with the boost fusion and mpl library and got
I recently started to play around with Amazon SES. When I intentionally tried to
I started to play around with knockout.js and in doing so I used the
I just started to play around with MongoDB (C#) and tried to port a
So I have started to play around with MonoTouch. OS X 10.6.8 MonoTouch v4.0.3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.