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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:59:27+00:00 2026-05-18T23:59:27+00:00

Now, I haven’t applied myself to functional programming for, oh, nearly 20 years, when

  • 0

Now, I haven’t applied myself to functional programming for, oh, nearly 20 years, when we didn’t get much further than writing factorials and fibs, so I’m really appealing to the community for some help in finding a solution.

My problem is this:

“Given a group of trade objects, I want to find all the combinations of trades that net to zero +/- some tolerance.”

My starter for ten is:

let NettedOutTrades trades tolerance = ...

Let’s assume my starting point is a previously constructed array of tuples (trade, value). What I want back is an array (or list, whatever) of arrays of trades that net out. Thus:

let result = NettedOutTrades [| (t1, -10); (t2, 6); (t3, 6); (t4; 5) |] 1

would result in:

   [| 
     [| t1; t2; t4 |]
     [| t1; t3; t4 |]
   |]

I’m thinking that this could be achieved with a tail recursive construct, using two accumulators – one for the results and one for the sum of trade values. But how to put it all together…?

I’m sure I could knock out something procedural using c#, but it just doesn’t feel like the right tool for the job – I’m convinced there’s going to be an elegant, concise, efficient solution using the functional paradigm…I’m just not well practiced enough to identify it at present!

  • 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-18T23:59:28+00:00Added an answer on May 18, 2026 at 11:59 pm

    Since @Tomas already gave a direct solution, I thought I’d present a solution which highlights composition with higher-order functions as a powerful technique commonly used in functional programming; this problem can be decomposed into three discrete steps:

    1. Generate all combinations of a set of elements. This is the most difficult and reusable piece of the problem. Therefore, we isolate this part of the problem into a stand-alone function which returns a sequence of combinations given a generic list of elements.
    2. Given list of (trade,value), filter out all combinations with value sums not within a given tolerance.
    3. Map each combination from a list of (trade,value) to a list of trade.

    I lifted @Tomas’s underlying algorithm for calculating all (expect the empty) combinations
    of a set but use a recursive sequence expression instead of a recursive function with an accumulator (I find this slightly easier to read and write).

    let combinations input =
        let rec loop remaining current = seq {
            match remaining with 
            | [] -> ()
            | hd::tail -> 
                yield  hd::current
                yield! loop tail (hd::current)
                yield! loop tail current
        }
        loop input []
    
    let nettedOutTrades tolerance trades =
        combinations trades
        |> Seq.filter
            (fun tradeCombo -> 
                tradeCombo |> List.sumBy snd |> abs <= tolerance)
        |> Seq.map (List.map fst)
    

    I swapped the order of trades and tolerance in your proposed function signature, since it makes it easier to curry by tolerance and pipe in (trade,value) lists which is the typical style used in the F# community and generally encouraged by the F# library. e.g.:

    [("a", 2); ("b", -1); ("c", -2); ("d", 1)] |> nettedOutTrades 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I haven't done much multithreading before and now find the need to do some
I have searched for hours now and haven't found a solution for my problem.
I've been using C# for a while now but haven't really homed in my
I haven't written anything with GDI for a while now (and never with GDI+),
I've been making small scale projects for a while now. I haven't started a
I have been struggling with this question for awhile now, and I haven't reached
I have been testing my geolocation query for some time now and I haven't
I've looked around for this for a while now and haven't really found anything
I've been searching the net for a couple days now and haven't turned up
I'm a naughty programmer, and until now I haven't handled errors properly (e.g. just

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.