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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:34:37+00:00 2026-05-31T17:34:37+00:00

I have a data constructor with a few value constructors: data DataType = C1

  • 0

I have a data constructor with a few value constructors:

data DataType = C1 | C2 | C3 | ... | Cn

I’d like to build a function at run time from that data type to some other values (in fact, I’m doing this in an IO monad):

buildFun :: IO (DataType -> b)
buildFun = do
    ....
    return $ \x -> case x of
                       C1 -> someProcessesToGetTheValue C1
                       ...
                       Cn -> someProcessesToGetTheValue Cn

Will this mean that someProcessesToGetTheValue will be called each time I call the returned function?

I’d prefer Haskell to evaluate someProcessesToGetTheValue inside buildFun (since those calls are quite expensive) and return a function which returns these fully evaluated expressions.

Can I force that behaviour? Perhaps by doing something like the following?:

buildFun :: IO (DataType -> b)
buildFun = do
    C1value <- return $ someProcessesToGetTheValue C1
    ...
    Cnvalue <- return $ someProcessesToGetTheValue Cn
    return $ \x -> case x of
                       C1 -> C1value
                       ...
                       Cn -> Cnvalue
  • 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-31T17:34:38+00:00Added an answer on May 31, 2026 at 5:34 pm

    You don’t have to involve the IO monad at all (and indeed do { x <- return v; ... } is identical to let x = v in ...), just bind the values outside the lambda:

    buildFun :: IO (DataType -> b)
    buildFun = do
        let v1 = someProcessesToGetTheValue C1
        ...
        return $ \x -> case x of { C1 -> v1; ... }
    

    Haskell doesn’t really specify anything about runtime evaluation behaviour, but on all common implementations this will ensure that the results are shared; see What does "floated out" mean? for more information.

    However, it still won’t evaluate v1…vn inside buildFun; instead, they will each be evaluated the first time the corresponding result of the function you return is evaluated. If you want to force them to be evaluated up-front, you can say let !v1 = someProcessesToGetTheValue C1 (this requires the BangPatterns language extension), or v1 <- evaluate $ someProcessesToGetTheValue C1 (from Control.Exception; this behaves better if someProcessesToGetTheValue C1 might throw an exception).

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

Sidebar

Related Questions

I have data in an MYSQL database that looks like this: Project Date Time
I have data that looks like CUSTOMER, CUSTOMER_ID, PRODUCT ABC INC 1 XYX ABC
I have data from a table in a database (string) that contain text and
I have data that looks like this: entities id name 1 Apple 2 Orange
I have data coming from the database in the form of a DataSet .
I have data from MySQL showing all organisations a customer got, with all details
I have data that needs to be executed on a certain background thread. I
Right now I have a few services that are defined in an assembly that
Most projects have some sort of data that are essentially static between releases and
I have a data structure that I want to access / modify in different

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.