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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:40:45+00:00 2026-06-06T20:40:45+00:00

I would like to make some of my code more monadic and to use

  • 0

I would like to make some of my code more “monadic” and to use a Reader monad rather than passing around a common environment. But somewhere I have to use mutable vectors, and hence the ST monad as well; to make things interesting, the ST action needs to access the environment (but the rest of the function does not). In other words, this works:

aux :: Int -> Reader Env Double
aux i = -- something

bla :: [a] -> Reader Env Double
bla l = do e <- ask
           return $ runST $ do -- something producing an Int
                               let o = runReader (aux i) e
                               -- something else depending on o
                               return something

but it feels very ugly and wrong, kind of a convoluted way to still pass the environment explicitly; I would like bla to look more like this:

bla :: [a] -> Reader Env Double
bla l = return $ runST $ do -- something producing an Int
                            o <- ??? aux i
                            -- something depending on o
                            return something

Does it make sense? Is it possible? If yes, what do I have to put instead of ??? ? I guess the true question would be, what is a good way to achieve this kind of thing? What design would you recommend?

  • 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-06T20:40:47+00:00Added an answer on June 6, 2026 at 8:40 pm

    You can mix Reader with ST using ReaderT. Something like that:

    import Data.Array.ST
    import Control.Monad.ST
    import Control.Monad.Reader
    
    type Env = String
    
    type ReaderST s = ReaderT Env (ST s)
    
    foo :: ReaderST s [Int]
    foo = do
      str <- ask
      let len = length str
      a <- lift (newArray (0, len - 1) 0 :: ST s (STUArray s Int Int))
      a' <- lift $ mapArray (+ 2) a
      es <- lift $ getElems a'
      return es
    
    run :: [Int]
    run = runST $ runReaderT foo "xyz"
    
    -- > run
    -- [2,2,2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently building a project and I would like to make use of some
I'm developing a free application and would like to make some money by embedding
I'm trying to make some input fields that I would like to work like
I would like to make my application ask the user some informations via something
I would like to make a form in HTML where a user enters some
I have a folder with some dotfiles I would like to make symlinks for.
I have some data in .cvs. I would like to make a simple barplot
I have some code that is basically 5 print statements that i would like
I am writing a numerical code, in which I would like to use a
I would like to be able to pull some code samples out of a

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.