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

  • Home
  • SEARCH
  • 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 8756347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:03:41+00:00 2026-06-13T14:03:41+00:00

Let State’ be my program state with some data. type State’ m a =

  • 0

Let State' be my program state with some data.

type State' m a = StateT Int m a

I would use it in some computations.

Examples:

-- genData, return some string (using Int value and State')
genData :: Int -> State' String
genData n = ...

-- genDatas, return multiple strings
genDatas :: Int -> State' [String]
genDatas n = mapM genData [1..n]

-- printLog, write log message (enumerating lines)
printLog :: String -> State' IO ()
printLog msg = do
  n <- get
  let n' = n + 1
  put n'
  liftIO $ putStrLn $ "Message #" ++ (show n') ++ ": " ++ msg

I think is not the correct way do it:

-- If I need a "in context function" returning a Int value...
--
-- addExtra, return current Int in state plus x
addExtra :: Int -> State' Identity Int
addExtra x = get >>= return.(+x)

To use my addExtra function in some monad context I do it:

doComplex :: State' IO ()
doComplex = do
  printLog "Starting process..."

  -- It's ugly!
  s <- get
  Identity (w, s') <- return $ runStateT (addExtra 5) s
  put s' -- save state

  printLog $ "computed value: " ++ (show w)

What is the correct way to share my State' monad along different functions? (as IO a is doing)

Thank you!

(I have read some tutorials and some source codes but I can’t understand it)

  • 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-06-13T14:03:42+00:00Added an answer on June 13, 2026 at 2:03 pm

    Answering your question

    Since your addExtra function doesn’t actually do anything with the underlying monad, you can just change the type signature to make it monad-agnostic:

    addExtra :: Monad m => Int -> State' m Int
    addExtra x = get >>= return . (+x)
    

    Now you can write this

    doComplex :: State' IO ()
    doComplex = do
        printLog "Starting process..."
    
        w <- addExtra 5
    
        printLog $ "Computed value: " ++ show w
    

    Which is much prettier, and works just like your old code:

    *Main> runStateT doComplex 0
    Message #1: Starting process...
    Message #2: Computed value: 6
    ((),2)
    

    Aside

    I’d probably be tempted to rewrite addExtra to one of the following. First, either use do notation

    addExtra x = do s <- get
                    return (s + x)
    

    or to use liftM, since we’re not really using the fact that we have a monad

    addExtra x = liftM (+x) get
    

    or even to use gets (thanks to Daniel Wagner in the comments)

    addExtra x = gets (+x)
    

    Of course, by this point you probably don’t need the additional function. You may as well just write

    doComplex = do printLog "Starting process..."
                   w <- gets (+5)
                   printLog $ "Computed value: " ++ show w
    

    Similarly, I’d probably rewrite printLog. If you ever find yourself geting the state, doing something to it, and puting it back, you probably just want to use modify.

    printLog msg = do modify (+1)
                      n <- get
                      liftIO . putStrLn $ "Message #" ++ show n ++ ": " ++ msg
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a table of State (varchar), City (varchar) and population (int)
I would like to set up a persistent state for my application. Let me
Let's consider state machines in VHDL that sit in some idle state until they're
Let me state up front that I have an infantile understanding of Monads. I
Let me state off the bat that I'm not that familiar with ASP.Net MVC,
First let me state that, despite being a fairly new practitioner of TDD, I'm
Before I ask the question let me state that I have attempted to google
First, let me state that this is a programming question (and thus does not
I state that I am a complete beginner to LDAP. I have to let
In DDD you should never let your entities enter an invalid state. That being

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.