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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:25:39+00:00 2026-05-26T20:25:39+00:00

I coded a function to enumerate all permutations of a given list. What do

  • 0

I coded a function to enumerate all permutations of a given list. What do you think of the code below?

def interleave(x:Int, l:List[Int]):List[List[Int]] = {
  l match { 
    case Nil => List(List(x))
    case (head::tail) =>
      (x :: head :: tail) :: interleave(x, tail).map(head :: _)
  }
}

def permutations(l:List[Int]):List[List[Int]] = {
  l match {
    case Nil => List(List())
    case (head::tail) =>
      for(p0 <- permutations(tail); p1 <- interleave(head, p0)) yield p1
  }
}
  • 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-26T20:25:39+00:00Added an answer on May 26, 2026 at 8:25 pm

    Given a Seq, one can already have permutations by invoking the permutations method.

    scala> List(1,2,3).permutations.mkString("\n")
    res3: String = 
    List(1, 2, 3)
    List(1, 3, 2)
    List(2, 1, 3)
    List(2, 3, 1)
    List(3, 1, 2)
    List(3, 2, 1)
    

    Furthermore there is also a method for combinations:

    scala> List(1,2,3).combinations(2).mkString("\n")
    res4: String = 
    List(1, 2)
    List(1, 3)
    List(2, 3)
    

    Regarding your implementation I would say three things:

    (1) Its good looking

    (2) Provide an iterator (which is the std collections approach that allows to discard elements). Otherwise, you can get lists with 1000! elements which may not fit in memory.

    scala> val longList = List((1 to 1000):_*)
    longList: List[Int] = List(1, 2, 3,...
    
    
    scala> permutations(longList)
    java.lang.OutOfMemoryError: Java heap space
        at scala.collection.immutable.List.$colon$colon(List.scala:67)
        at .interleave(<console>:11)
        at .interleave(<console>:11)
        at .interleave(<console>:11)
    

    (3) You should remove duplicated permutations (as observed by Luigi), since :

    scala> permutations(List(1,1,3))
    res4: List[List[Int]] = List(List(1, 1, 3), List(1, 1, 3), List(1, 3, 1), List(1, 3, 1), List(3, 1, 1), List(3, 1, 1))
    
    scala> List(1,1,3).permutations.toList
    res5: List[List[Int]] = List(List(1, 1, 3), List(1, 3, 1), List(3, 1, 1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have coded the next function. But surely someone has a more elegant way
I want to call a function from a .NET DLL (coded in C#) from
I'm writing a function that replaces long hex coded color ( #334455 ) with
I have coded a view that relies on a scalar function value for one
This code function LoadContent(Id) { alert('Controls/Network/NetworkDetail.aspx?' + rnd() + '&CtlId=' + Id); $.get('Controls/Network/NetworkDetail.aspx?' +
The code: function updateDashboardData() { $.getJSON(includes/system/ajaxDataInterface.php, {recordcount:1}, function(data) { $('.stationContainer').each(function(data) { var bsID =
View this code: function testprecision(){ var isNotNumber = parseFloat('1.3').toPrecision(6); alert(typeof isNotNumber); //=> string }
I have this code: function submitTwice(f){ f.action = 'http://mydomain.com/threevideopage.php'; f.target='name'; f.submit(); } I left
Here is my code: function pauseSound() { var pauseSound = document.getElementById(backgroundMusic); pauseSound.pause(); } I
With this code function someFunction(classParam:Class):Boolean { // how to know if classParam implements some

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.