Created
July 4, 2014 03:55
-
-
Save hsk/394891d5cf7b57ba4231 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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