365
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] =
|
564
|
2 |
for (x <- A ; y <- B) yield x ++ y
|
365
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
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 {
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case 0 => Set("")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
case n => concat(A, pow(A, n- 1))
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
676
|
9 |
val A = Set("aa", "a")
|
|
10 |
val B = Set("aaa", "aaaa")
|
|
11 |
concat(A, B).size // -> 3
|
669
|
12 |
|
|
13 |
|
|
14 |
|
650
|
15 |
val A = Set("1", "2", "3", "")
|
|
16 |
val B = Set("1", "2", "3", "4", "5", "6", "")
|
|
17 |
concat(A, B).size // -> 28
|
|
18 |
pow(B, 3).size
|
|
19 |
|
|
20 |
|
365
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
val A = Set("a", "b", "c", "d")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
pow(A, 4).size // -> 256
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
val B = Set("a", "b", "c", "")
|
572
|
25 |
pow(B, 4)
|
365
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
pow(B, 4).size // -> 121
|
638
|
27 |
pow(B, 3).size
|
471
|
28 |
|
|
29 |
|
397
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
30 |
val B2 = Set("a", "b", "c", "")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
31 |
pow(B2, 3).size // -> 40
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
32 |
|
365
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
val C = Set("a", "b", "")
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
pow(C, 2)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
pow(C, 2).size // -> 7
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
pow(C, 3)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
pow(C, 3).size // -> 15
|
525
|
39 |
|
|
40 |
|
572
|
41 |
//val A = Set("a", "b", "c", "d")
|
|
42 |
//pow(A, 4).size
|
525
|
43 |
|
572
|
44 |
//val A = Set("a", "b", "c")
|
|
45 |
//pow(A, 5).size
|
525
|
46 |
|
572
|
47 |
//val A = Set("a", "b", "")
|
|
48 |
//pow(A, 5).size
|
564
|
49 |
|
|
50 |
|
572
|
51 |
for (n <- (0 to 6).toList)
|
|
52 |
yield pow(B, n).size
|
577
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
val A = Set("a", "b", "c")
|
|
59 |
pow(A, 3)
|
618
|
60 |
pow(A, 3).size
|
577
|
61 |
|
|
62 |
val B = Set("a", "b", "")
|
|
63 |
pow(B, 4)
|
618
|
64 |
pow(B, 4).size
|
765
|
65 |
|
768
|
66 |
val C = Set("a", "")
|
|
67 |
pow(C, 4)
|
|
68 |
pow(C, 4).size
|
765
|
69 |
|
|
70 |
val SA = Set("a", "b", "c", "d")
|
|
71 |
val SB = Set("0", "1", "2", "3", "4", "5", "6")
|
|
72 |
|
|
73 |
concat(SA, SB).size
|
|
74 |
|
|
75 |
|
|
76 |
val SA2 = Set("ab", "a", "c", "d")
|
|
77 |
val SB2 = Set("bc", "c", "2", "3", "4", "5", "6")
|
|
78 |
|
|
79 |
concat(SA2, SB2).size
|
|
80 |
|
|
81 |
|
|
82 |
/*
|
|
83 |
((a b) c) d
|
|
84 |
a (b (c d))
|
|
85 |
(a b) (c d)
|
|
86 |
(a (b c)) d
|
|
87 |
a ((b c) d)
|
|
88 |
*/
|