Skip to content

Instantly share code, notes, and snippets.

@hsk
Created July 4, 2014 03:55
Show Gist options
  • Save hsk/394891d5cf7b57ba4231 to your computer and use it in GitHub Desktop.
Save hsk/394891d5cf7b57ba4231 to your computer and use it in GitHub Desktop.
package set
object main extends App {
def power[A](t: Set[A]): Set[Set[A]] = {
@annotation.tailrec
def pwr(t: Set[A], ps: Set[Set[A]]): Set[Set[A]] =
if (t.isEmpty) ps
else pwr(t.tail, ps ++ (ps map (_ + t.head)))
pwr(t, Set(Set.empty[A])) //Powerset of ∅ is {∅}
}
val a = Set(1,2,3)
val b = Set(3,4,5,6)
println("|a| = "+a.size)
println("|b| = "+b.size)
println("a ∪ b = "+(a union b))
println("a ∩ b = "+(a intersect b))
println("a \\ b = "+(a diff b))
println(power(Set(1,2,3)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment