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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:14:46+00:00 2026-05-12T01:14:46+00:00

I went through some tutorials on the State monad and I think I got

  • 0

I went through some tutorials on the State monad and I think I got the idea.

For example, as in this nice tutorial:

import Data.Word

type LCGState = Word32

lcg :: LCGState -> (Integer, LCGState)
lcg s0 = (output, s1) 
  where s1 = 1103515245 * s0 + 12345
        output = fromIntegral s1 * 2^16 `div` 2^32


getRandom :: State LCGState Integer
getRandom = get >>= \s0 -> let (x,s1) = lcg s0
                           in put s1 >> return x

OK, so I can use getRandom:

*Main> runState getRandom 0
(0,12345)
*Main> runState getRandom 0
(0,12345)
*Main> runState getRandom 1              
(16838,1103527590)

But I still need to pass the seed to the PRNG every time I call it. I know that the
PRNG available in Haskell implementations does not need that:

Prelude> :module Random
Prelude Random> randomRIO (1,6 :: Int)
(...) -- GHC prints some stuff here
6
Prelude Random> randomRIO (1,6 :: Int)
1

So I probably misunderstood the State monad, because what I could see in most tutorials
doesn’t seem to be “persistent” state, but just a convenient way to thread state.

So… How can I have state that is automatically initialized (possible from some
function that uses time and other not-very-predictable data), like the Random module
does?

Thanks a lot!

  • 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-12T01:14:47+00:00Added an answer on May 12, 2026 at 1:14 am

    randomRIO uses the IO monad. This seems to work nicely in the interpreter because the interpreter also works in the IO monad. That’s what you are seeing in your example; you can’t actually do that at the top-level in code — you would have to put it in a do-expression like all monads anyway.

    In general code you should avoid the IO monad, because once your code uses the IO monad, it is tied to external state forever — you can’t get out of it (i.e. if you have code that uses the IO monad, any code that calls it also has to use the IO monad; there is no safe way to “get out” of it). So the IO monad should only be used for things like accessing the external environment, things where it is absolutely required.

    For things like local self-contained state, you should not use the IO monad. You can use the State monad as you have mentioned, or you can use the ST monad. The ST monad contains a lot of the same features as the IO monad; i.e. there is STRef mutable cells, analogous to IORef. And the nice thing about ST compared to IO is that when you are done, you can call runST on an ST monad to get the result of the computation out of the monad, which you can’t do with IO.

    As for “hiding” the state, that just comes as part of the syntax of do-expressions in Haskell for monads. If you think you need to explicitly pass the state, then you are not using the monad syntax correctly.

    Here is code that uses IORef in the IO Monad:

    import Data.IORef
    foo :: IO Int -- this is stuck in the IO monad forever
    foo = do x <- newIORef 1
             modifyIORef x (+ 2)
             readIORef x
    -- foo is an IO computation that returns 3
    

    Here is code that uses the ST monad:

    import Control.Monad.ST
    import Data.STRef
    bar :: Int
    bar = runST (do x <- newSTRef 1
                    modifySTRef x (+ 2)
                    readSTRef x)
    -- bar == 3
    

    The simplicity of the code is essentially the same; except that in the latter case we can get the value out of the monad, and in the former we can’t without putting it inside another IO computation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Seems like jQuery LocalScroll. From their pushup.js (indented for clarity):… May 12, 2026 at 10:05 am
  • Editorial Team
    Editorial Team added an answer The void operator evaluates the given expression and then returns… May 12, 2026 at 10:05 am
  • Editorial Team
    Editorial Team added an answer Let's formulate what you want. You want target named `cnt'… May 12, 2026 at 10:05 am

Related Questions

I went through a period of being interested in how quantum computers work and
I just went through some MVC tutorials after checking this site out for a
I'm looking at some GXT code for GWT and I ran across this use
I've wanted to learn Ruby for some time and even started to learn a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.