progs/pow.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 30 Sep 2018 12:01:14 +0100
changeset 564 b5d57d7064bb
parent 525 a2ee4b11c976
child 572 4a1739f256fd
permissions -rw-r--r--
updated

def concat(A: Set[String], B: Set[String]) : Set[String] =
  for (x <- A ; y <- B) yield x ++ y

def pow(A: Set[String], n: Int) : Set[String] = n match {
  case 0 => Set("")
  case n => concat(A, pow(A, n- 1))
}

val A = Set("a", "b", "c", "d")
pow(A, 4).size                            // -> 256

val B = Set("a", "b", "c", "")
pow(B, 4).size                            // -> 121



val B2 = Set("a", "b", "c", "")
pow(B2, 3).size                           // -> 40

val C = Set("a", "b", "")
pow(C, 2)
pow(C, 2).size                            // -> 7

pow(C, 3)
pow(C, 3).size                            // -> 15


val A = Set("a", "b", "c", "d")
pow(A, 4).size   

val A = Set("a", "b", "c")
pow(A, 5).size   

val A = Set("a", "b", "")
pow(A, 5).size   


for (n <- (0 to 12).toList) 
  yield pow(A, n).size