Skip to content

Instantly share code, notes, and snippets.

@meysampg
Created July 16, 2020 15:20
Show Gist options
  • Save meysampg/eb3bf9a1e68168824dc6083e093a8585 to your computer and use it in GitHub Desktop.
Save meysampg/eb3bf9a1e68168824dc6083e093a8585 to your computer and use it in GitHub Desktop.
import scala.annotation.tailrec
val a = List(1, 2, 3, 4)
@tailrec
def powerset(current: List[List[Int]], given: List[Int]): List[List[Int]] = given match {
case Nil => current
case head :: tail => powerset(current ++ (current map {x => head::x}), tail)
}
for (i <- powerset(List(Nil), a)) {
println(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment