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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:18:24+00:00 2026-06-05T20:18:24+00:00

I have a module where a global environment (defining certain constraints such as neighbor

  • 0

I have a module where a global environment (defining certain constraints such as neighbor IP addresses, etc.) is created and initialized by calling an initializing function. A number of subsequent functions should use these constraints when they are called.

Whilst in principle I understand what the reader monad does I am not quite sure how I can apply this to my problem, esp.

  • How it can be used to initialize an environment that is defined by the user and passed as data/arguments to the initializing function. I mean, the reader monad has to get the actual values that make up the global immutable environment from somewhere. I would like that the values are read from an initializing function call like myinitial :: arg1 -> arg1 -> IOString where subsequently arg1 and arg2 become global immutable data accessible to subsequent functions via the reader monad(?)

  • How I can use these environment values as function arguments e.g. recvFrom s arg1 where arg1 is global immutable data from my environment. Or if arg2 > arg1 then ... else ...

I could make a configuration file of course, but I feel that a configuration file will take away to much flexibility.

[Edit] I understand about ask, but shouldn’t there be additional “pointfree-like” ways so that the global/environment immutable can be omitted if the function signature has been defined right? How would I i.e. need to refactor my if-then-else to apply this.

  • 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-05T20:18:25+00:00Added an answer on June 5, 2026 at 8:18 pm

    Here’s an example that may clear things up. First you need to import the Reader module:

    import Control.Monad.Reader
    

    Now let’s define some data structure (that we’re going to use to hold a name and an age)

    data Config = Config { name :: String, age :: Int }
    

    Now define a function that works in the Reader monad (it’s type is Reader Config (String, Int) but we don’t need to specify that – it can be inferred). All this function does is ask for the environment (of type Config) and then extracts the fields and does something with them.

    example = do
        c <- ask
        return ("Hello " ++ name c, 2 * age c)
    

    Now we put it all together into a program. The first four lines after the do block allow the user to enter their name and age. Then we build a Config structure using the user’s inputs (we have to use read to convert the variable _age, which is a String, into an Int so that we can feed it to the Config constructor) and execute example with this environment, using the runReader function. Finally, we use the result of this computation to generate some output.

    main = do
        putStrLn "Enter your name:"
        _name <- getLine
        putStrLn "Enter your age:"
        _age <- getLine
        let config = Config _name (read _age)
        let result = runReader example config
        putStrLn $ fst result
        putStrLn $ "Twice your age is: " ++ show (snd result)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary called fsdata at module level (like a global variable). The
I have a module modA , which contains a synthesized submodule modB (created with
I have a module in which I created a custom page with controller and
I have a module containing multiple global functions, and a global variable. The variable
Possible Duplicate: How to programmatically set a global (module) variable? I have a class
I have a Global.ascx and I wrote a simple url writer module : void
I have a Python module with nothing but regular global functions. I need to
I have a python module that makes use of a huge dictionary global variable,
I have a module named module.py, which checks a global variable in context. module.py:
I have module application. When I run it, the main window of that app

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.