progs/pow.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:58:18 +0000
changeset 975 ae5c03560d4d
parent 965 94f5cce73a4f
permissions -rw-r--r--
update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
965
94f5cce73a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
     1
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
def concat(A: Set[String], B: Set[String]) : Set[String] =
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
     3
  for (x <- A ; y <- B) yield x ++ y
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
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
     6
  case 0 => Set("")
882
5fcad75ade92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 801
diff changeset
     7
  case n => concat(A, pow(A, n - 1))
365
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
650
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    10
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    11
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
val A = Set("a", "b", "c", "d")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
val B = Set("a", "b", "c", "")
965
94f5cce73a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
    14
pow(A, 4).size
94f5cce73a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
    15
pow(B, 4).size
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    16
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    17