author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 26 Nov 2015 12:55:59 +0000 | |
changeset 387 | 8aa406adfde0 |
parent 365 | 9b71dead1219 |
child 397 | cf3ca219c727 |
permissions | -rw-r--r-- |
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 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
val C = Set("a", "b", "") |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
pow(C, 2) |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
pow(C, 2).size // -> 7 |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
|
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
pow(C, 3) |
9b71dead1219
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
pow(C, 3).size // -> 15 |