Code to enumerate permutations in Scala

后端 未结 11 2075
臣服心动
臣服心动 2020-12-02 16:06

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]] = {         


        
11条回答
  •  孤城傲影
    2020-12-02 16:41

    Consider the difference here: your version

    scala> permutations(List(1,1,2)) foreach println
    List(1, 1, 2)
    List(1, 1, 2)
    List(1, 2, 1)
    List(1, 2, 1)
    List(2, 1, 1)
    List(2, 1, 1)
    

    The reference version:

    scala> List(1,1,2).permutations foreach println
    List(1, 1, 2)
    List(1, 2, 1)
    List(2, 1, 1)
    

提交回复
热议问题