progs/pow.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 19 Dec 2015 21:11:23 +0000
changeset 392 2d0a59127694
parent 365 9b71dead1219
child 397 cf3ca219c727
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 C = Set("a", "b", "")
pow(C, 2)
pow(C, 2).size                            // -> 7

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