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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:36:14+00:00 2026-06-10T07:36:14+00:00

I want to use Haskell to solve a financial combinatorial problem, the list monad

  • 0

I want to use Haskell to solve a financial combinatorial problem, the list monad seems to be a good fit for this.

Now, my problem with the list monad is its inability to give names to the values involved. I will try to exemplify:

loan = [1000*x | x <- [1..3]]
interest_rate = [0.005*x | x <- [4..10]]

calc = do                                                                                    
  l <- loan                                                                                  
  i <- interest_rate                                                                         
  return (l*i)

Running calc above gives me a list of numbers ([20.0,25.0,30.0,35.0,40.0, ... ]), but I can’t tell what the loan and interest rate is used for each calculation.

I get lost here, my intuition tells me to create my own monadic type of, say HelpfulNumber :: (String,[Double]) and somehow say that:

>>= and return should be >>= . snd and return . snd

Am I on the right course here, or is there a better way? I am feeling a bit lost to be honest.

  • 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-10T07:36:15+00:00Added an answer on June 10, 2026 at 7:36 am

    You could use a record type to make your output clearer:

    data Loan = Loan {final :: Double, 
                      rate  :: Double, 
                      loan  :: Integer, 
                      years :: Int}
       deriving Show
    
    printloans :: [Loan] -> IO()
    printloans = mapM_ print
    

    Use printloans loans or printloans loans' at the ghci prompt.

    Edit: I forgot to include the definition of dp. It’s for rounding to a given number of decimal places:

    dp :: Int -> Double -> Double
    n `dp` a = (/ 10.0^n).fromInteger.round.(* 10.0^n) $ a
    

    Here’s a way using a list directly:

    loans = [Loan {final = (2 `dp`) $ fromInteger amt*(1+ir)^yrs, 
                   rate  = ir,
                   loan  = amt,
                   years = yrs}  
            | ir <- [0.005*x | x <- [4..10]],
              amt <- [1000*x | x <- [1..3]],
              yrs <- [1..4]
            ]
    

    But if you like the monadic style, you can use:

    loans' = do
      ir <- [0.005*x | x <- [4..10]]
      amt <- [1000*x | x <- [1..3]]
      yrs <- [1..4]
      return Loan {final = (2 `dp`) $ fromInteger amt*(1+ir)^yrs, 
                   rate  = ir,
                   loan  = amt,
                   years = yrs}
    

    which benefits from fewer commas, and it’s easier to change the order of the <- lines to change the order of the answers.
    You can add extras to your Loan record and calculate with them.
    You get output like this:

    *Main> printloans loans'
    Loan {final = 1020.0, rate = 2.0e-2, loan = 1000, years = 1}
    Loan {final = 1040.4, rate = 2.0e-2, loan = 1000, years = 2}
    Loan {final = 1061.21, rate = 2.0e-2, loan = 1000, years = 3}
    Loan {final = 1082.43, rate = 2.0e-2, loan = 1000, years = 4}
    Loan {final = 2040.0, rate = 2.0e-2, loan = 2000, years = 1}
    Loan {final = 2080.8, rate = 2.0e-2, loan = 2000, years = 2}
    ...
    ...
    

    EDIT:

    You told me elsewhere you’d like output like ir_5% yrs_3 amt_4000 tot_4360.5. It’s uglier, but here’s a way of doing that sort of thing:

    loans'' = do
      ir <- [0.005*x | x <- [4..10]]
      amt <- [1000*x | x <- [1..3]]
      yrs <- [1..4]
      let final = (2 `dp`) $ fromInteger amt*(1+ir)^yrs
      return $ "final_" ++ show final
            ++ ",  ir_" ++ show ((2 `dp`) $ ir*100.0)    -- rounded away a rounding error in 3.5% 
            ++ "%,  amt_" ++ show amt 
            ++ ",  yrs_" ++ show yrs
    

    When you do mapM_ putStrLn loans'' you get output like

    final_1020.0,  ir_2.0%,  amt_1000,  yrs_1
    final_1040.4,  ir_2.0%,  amt_1000,  yrs_2
    final_1061.21,  ir_2.0%,  amt_1000,  yrs_3
    final_1082.43,  ir_2.0%,  amt_1000,  yrs_4
    final_2040.0,  ir_2.0%,  amt_2000,  yrs_1
    ....
    

    but I think the record type is much nicer – its output is easier to read and there’s less messing about with strings.

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

Sidebar

Related Questions

I read this: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns I like the idea, want to use the extension. I
Trying to solve problem 23 of 99 Haskell Problems. And I wrote this rnd_select
I want use this 1 for using Bar code or QR code scanner. I
We have a powerbuilder application and we want use a scanner through this application
I want to use a Haskell function with the following type :: string ->
I want to call haskell functions out of C++ and did use the tutorial
In Haskell, I use the Data.Map module, and its principal type of the same
I want to use Haskell in production. It has a lot of libraties but
I want to use some Haskell libraries (e.g. Darcs, Pandoc) from Python, but it
I've got a name clash between two different Haskell modules that want to use

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.