progs/pow.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 29 Oct 2016 21:45:44 +0100
changeset 467 b5ec11e89768
parent 397 cf3ca219c727
child 471 9476086849ad
permissions -rw-r--r--
fixed bug

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