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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:54:22+00:00 2026-06-02T19:54:22+00:00

I found a function for creating a cartesian product from a list of lists

  • 0

I found a function for creating a cartesian product from a list of lists in Scala. However, it isn’t tail recursive and won’t work well with large lists. Unfortunately I won’t know at design time how many lists I will need to combine, so I believe a recursive function is necessary. I’m struggling to make it tail recursive so it can be optimized by the compiler:

def product[T](listOfLists: List[List[T]]): List[List[T]] = listOfLists match {
    case Nil => List(List())
    case xs :: xss => for (y <- xs; ys <- product(xss)) yield y :: ys
}
  • 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-02T19:54:24+00:00Added an answer on June 2, 2026 at 7:54 pm

    This approach is similar to your original method except that instead of starting and the front and recursively descending until you get to the end and appending back up, I’ve introduced an accumulator to that I can just march through the list backwards, accumulating as I go.

    import annotation.tailrec
    
    def product[T](listOfLists: List[List[T]]): List[List[T]] = {
      @tailrec def innerProduct[T](listOfLists: List[List[T]], accum: List[List[T]]): List[List[T]] =
        listOfLists match {
          case Nil => accum
          case xs :: xss => innerProduct(xss, for (y <- xs; a <- accum) yield y :: a)
        }
    
      innerProduct(listOfLists.reverse, List(Nil))
    }
    

    Then:

    scala> product(List(List(1,2),List(3,4)))
    res0: List[List[Int]] = List(List(1, 3), List(1, 4), List(2, 3), List(2, 4))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating an initialising function for the class 'Room', and found that the program
I used the following function I found online and it works perfectly. However, when
I found the following stored procedure from here , while creating this stored procedure
Is there an already existing function in PHP for creating an associative array from
I am creating a data-parallel program using pthreads and C++. From pthread function from
I'm reading some third party Verilog, and found this: function [31:0] factorial; input [3:0]
How to move custom marker to front on google map? I found moveToTop function
PHP5.1.6 has no json_encode(), so I wanted to use function found on json_encode documentation
I'm using a jquery function I found to find words in a div and
I'm learning JQuery and have found the factory function $() with its selectors quite

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.