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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:59:11+00:00 2026-05-23T03:59:11+00:00

I am trying to do a global counter using monads in Haskell, I want

  • 0

I am trying to do a global counter using monads in Haskell, I want to get the incremented value every time I use the monad counter, but I am kind of stuck getting the same value each time!
The code list is as follows:

module CounterMonad where

data Counter a = C (Int -> (Int, a))

--reset the counter
new :: Counter ()
new = C $ \_ -> (0, ())

-- increment the counter:
--inc :: Counter Int
--inc = C $ \n -> (n+1, n)
inc = get >>= \s -> (put (s+1))

-- returning the current value of the counter
get :: Counter Int
get = C $ \n -> (n, n)

--
put x = C $ \n -> (x, x)

--return is nop, >>= is sequential exectuion
instance Monad Counter where
    return r = C $ \n -> (n, r)
    (>>=) (C f) g = C $ \n0 -> let (n1, r1) = f n0
                                   C g' = g r1
                               in g' n1

run :: Counter a -> a
run (C f) = snd (f 0)


tickC  = do
  inc
  c <- get
  return c

When I try to execute as run tickC, it always returns 1.
What I want is everytime I run tickC, it returns the incremented value, like 1, ,2, 3,4 ….
I know there must be some stupid problem lying there, can you guys point out how?

  • 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-23T03:59:12+00:00Added an answer on May 23, 2026 at 3:59 am

    This is correct behavior. Every time you call run tickC, the run function evaluates the operation with the counter set to zero. Essentially, each time you call run you get a different “copy” of the counter initialized to zero.

    If you want to have the counter increment each time, you have to execute all operations in the same call to run. For example,

    tickMany = do
        x <- tickC
        y <- tickC
        z <- tickC
        return [x, y, z]
    
    > run tickMany
    [1, 2, 3]
    

    This is true of all monads, including IO (ignoring the “unsafe” operations). Since the only way to run an IO operation is through main, the IO monad is threaded through every function that uses it.

    So if you want a global counter, that counter has to be managed by a monad that is used globally (i.e., by every function that needs to access it). You can either use your Counter monad globally or put the counter in the IO monad. It seems to be accepted design practice to put state like this in your own monad, but of course, it depends on the application and IO is fine too.

    You may also wish to look at Control.Monad.State, which would allow you to define your monad with much less typing. Something like:

    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    import Control.Monad.State
    newtype Counter a = Counter (State Int a) deriving (Monad)
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the Global Interface Table by using the following code (Delphi):
I'm trying to get a global hotkey working in Linux using Mono. I found
Trying to pass a form value into a global var to be use in
I am trying to get the user defined global hot key for my application.
I'm trying to set global shortcut for my applcation using QxtGlobalShortcut. Here is my
I am trying to translate global array vector but whenever I assemble it I
I am trying to use a global in a file that is just used
I am trying to increment a counter, but I would like to move the
I'm trying to initialize a global array of function pointers at compile-time, in either
I'm trying to get the following global variable storage_i to be accessible to the

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.