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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:51:51+00:00 2026-06-18T02:51:51+00:00

I’m struggling to write the code, that will calculate sum of int list list.

  • 0

I’m struggling to write the code, that will calculate sum of int list list. For example, if we consider following list

[[1],[1,5],[7],[2,3,4]]

we can get different possible sums, depending on which integer we choose every time:

[11,12,13,15,16,17]

Can someone give possible ways (not necessary the code) I could solve it?

  • 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-18T02:51:52+00:00Added an answer on June 18, 2026 at 2:51 am

    This is my attempt to make tail-recursive (kinda) version of algorithm with using lists and pattern matching only. It is written in OCaml, not SML and requires ascending order of inner lists

    let reverse list = 
        let rec iter list acc :int list = match list with
        [] -> acc
        | x::xs -> iter xs (x::acc)
     in iter list [];;
    
    let listAdd num list = 
      let rec iter num list acc :int list = match list with
         [] -> acc
        | x::xs -> iter num xs ((x + num)::acc)
      in reverse( iter num list []);;   
    
    let listMerge listX listY = 
      let rec iter listX listY acc : int list = match listX with 
         [] -> (reverse acc) @ listY
         | x :: xs -> match listY with 
            [] -> (reverse acc ) @ listX
            | y :: ys -> match ( compare x y ) with
               0  ->  iter xs ys ( x::acc )
              |(-1) ->  iter xs listY ( x::acc )    
              | 1 ->  iter listX ys ( y::acc )
      in iter listX listY [];;  
    
    let listSums listX listY =
     let rec iter listX listY acc = match listX with 
       [] -> acc
       | x :: xs -> iter xs listY ( listMerge acc (listAdd x listY ) )
     in iter listX listY [];;  
    
    let possibleSums shallowList = match shallowList with 
       [] -> []
       | headList :: lists -> 
          let rec iter acc lists  = match lists with 
              [] -> acc
              | headList :: other -> iter ( listSums acc headList ) other  
          in iter headList lists;;
    

    here possibleSums [[1];[1;5];[7];[2;3;4]];; evaluates to int list = [11; 12; 13; 15; 16; 17]

    So basically logical steps are:

    • the reverse function is just an utility to restore order after resulting accumulator is built – usual concept for tail recursive optimisation
    • the listAdd function to avoid using map utility in constructing summaries
    • the listMerge function equal to set union to use sorted lists as integer sets
    • the listSums function for mapping summation to cartesian product of two sets
    • the posibleSums function for final level of mapping summation to full cartesian product of all sets.

    This algorithm is not just avoiding stack overflow in recursion through TCO, but also providing near optimal solution for this calculation problem.

    Also this solution can be further improved with adding premature merge sorting and removing repeating elements from sets, so inner set order is now insignificant:

    let split lst = 
        let rec iter lst partX partY = match lst with
            [] -> partX,partY
            | x::xs -> iter xs partY (x::partX)
        in iter lst [] [];;
    
    let rec mergeSort lst = match lst with 
        [] -> []
        |[x] -> [x]
        | _ -> let partX,partY = split lst in
               listMerge (mergeSort partX) (mergeSort partY);;
    
    let possibleSums shallowList = match shallowList with 
       [] -> []
       | headList :: lists -> 
          let rec iter acc lists  = match lists with 
              [] -> acc
              | headList :: other -> iter ( listSums acc (mergeSort headList) ) other  
          in iter (mergeSort headList) lists;;
    

    You can test its efficiency on simple example. Let’s define repeat function for making repetitive lists:

     let rec repeat elem n = match n with
         0 -> []
        | _ -> elem :: (repeat elem (n - 1));;
    

    In that case possibleSums( repeat( repeat 1 30) 30 ) will calculate correct singleton answer int list = [30] in no time. While more straightforward solutions (like Jesper’s one) will do it forever.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.