progs/pow.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 24 Sep 2018 11:05:39 +0100
changeset 558 447ed6c7cdad
parent 525 a2ee4b11c976
child 564 b5d57d7064bb
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
def concat(A: Set[String], B: Set[String]) : Set[String] =
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  for (x <-A ; y <- B) yield x ++ y
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
def pow(A: Set[String], n: Int) : Set[String] = n match {
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  case 0 => Set("")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
  case n => concat(A, pow(A, n- 1))
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
}
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
val A = Set("a", "b", "c", "d")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
pow(A, 4).size                            // -> 256
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
val B = Set("a", "b", "c", "")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
pow(B, 4).size                            // -> 121
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 397
diff changeset
    15
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 397
diff changeset
    16
397
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    17
val B2 = Set("a", "b", "c", "")
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    18
pow(B2, 3).size                           // -> 40
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    19
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
val C = Set("a", "b", "")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
pow(C, 2)
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
pow(C, 2).size                            // -> 7
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
pow(C, 3)
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
pow(C, 3).size                            // -> 15
525
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    26
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    27
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    28
val A = Set("a", "b", "c", "d")
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    29
pow(A, 4).size   
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    30
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    31
val A = Set("a", "b", "c")
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    32
pow(A, 5).size   
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    33
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    34
val A = Set("a", "b", "")
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    35
pow(A, 5).size