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 7192729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:00:11+00:00 2026-05-28T20:00:11+00:00

I am working on a simple example to implement the state monad in Haskell.

  • 0

I am working on a simple example to implement the state monad in Haskell. I have made a data type GlobState using record syntax. Functions incr1, incr2 and incr3 are there to increment the respective record elements. The code i have written is:

module StateExample where
import Control.Monad.State

data GlobState = GlobState { c1 :: Int, c2:: Int, c3:: Int} deriving (Show)

newGlobState:: GlobState
newGlobState = GlobState { c1=0,c2=0,c3=0 }

--incr1 :: State GlobState ()
incr1 = do
    gets c1
    modify (+1)

--incr2 :: State GlobState ()
incr2 = do
    gets c2
    modify(+1)

incr3 = do
    gets c3
    modify(+1)

main =  do
    let ((),a1) = flip runState newGlobState $ do
        x<- incr1
        y<- incr2 x
        z<- incr1 y
        return z
    print a1

This program is not giving the desired output. And i am not sure about the signature of increment functions or if gets is doing what i intend to do. Please help.

How can i make changes to the code so that instead of defining the different increment functions, i need to make only one increment function. i intend to do like:

incr :: String-> State GlobState ()
incr x = do
modify(\g -> g {x =x g + 1})
  • 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-28T20:00:11+00:00Added an answer on May 28, 2026 at 8:00 pm

    Your types are right, at least.

    incr1 :: State GlobState ()
    incr1 = modify (\state -> state {c1 = 1 + c1 state})
    

    Your modify (+1) would be fine if you were working in State Int (), say, but we’re not. You seem to think that gets focusses the following lines onto a particular field of the state — but it doesn’t.

    incr2 and incr3 need to be changed similarly.

    As to main:

    1. You need to indent your code properly. You appear to want the print a1 to be part of the outer do block, but not part of the inner do block. Without indentation, the compiler cannot figure that out.
    2. You are (attempting to) manually thread the state, but the whole point about State is that its monadic/applicative plumbing does that for you.
    main = do let ((), a1) = flip runState newGlobState $ do incr1
                                                             incr2
                                                             incr1
              print a1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple example I can't seems to get working : MERGE INTO
I'm working on a bigger project atm, but I made this simple example to
I am working on a simple chat application using a System.Windows.Forms.WebBrowser Control to display
I have an requirement to implement a custom keyboard for a Cocoa Touch data
I'm trying to implement a simple example of where ASP.NET changes culture. I'm running
Many areas in a project I'm working on have a simple timeout check which
I'm working on a simple note-taking/painting app. I have a view controller that works
I am working with libmcrypt in c and attempting to implement a simple test
Can someone explain the statement below to me with a working sample/example. thanks in
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.

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.