progs/pow.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 06 Oct 2021 20:43:35 +0100
changeset 842 b53ac1bb5f43
parent 801 7aab258bf72a
child 882 5fcad75ade92
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] =
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
     2
  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
     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
801
7aab258bf72a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 768
diff changeset
     9
val A = Set("a", "b", "c", "d", "e")
7aab258bf72a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 768
diff changeset
    10
val B = Set("a", "b", "c", "d", "")
7aab258bf72a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 768
diff changeset
    11
pow(A, 4).size
7aab258bf72a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 768
diff changeset
    12
pow(B, 4).size
7aab258bf72a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 768
diff changeset
    13
7aab258bf72a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 768
diff changeset
    14
676
62d168bf7ac8 ypdated
Christian Urban <urbanc@in.tum.de>
parents: 669
diff changeset
    15
val A = Set("aa", "a")
62d168bf7ac8 ypdated
Christian Urban <urbanc@in.tum.de>
parents: 669
diff changeset
    16
val B = Set("aaa", "aaaa")
62d168bf7ac8 ypdated
Christian Urban <urbanc@in.tum.de>
parents: 669
diff changeset
    17
concat(A, B).size                     // -> 3 
669
2f5a4d76756d updated
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    18
2f5a4d76756d updated
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    19
2f5a4d76756d updated
Christian Urban <urbanc@in.tum.de>
parents: 650
diff changeset
    20
650
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    21
val A = Set("1", "2", "3", "")
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    22
val B = Set("1", "2", "3", "4", "5", "6", "")
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    23
concat(A, B).size                     // -> 28 
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    24
pow(B, 3).size 
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    25
3031e3379ea3 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    26
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
val A = Set("a", "b", "c", "d")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
pow(A, 4).size                            // -> 256
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
val B = Set("a", "b", "c", "")
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    31
pow(B, 4)
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
pow(B, 4).size                            // -> 121
638
0367aa7c764b updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    33
pow(B, 3).size 
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 397
diff changeset
    34
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 397
diff changeset
    35
397
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    36
val B2 = Set("a", "b", "c", "")
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    37
pow(B2, 3).size                           // -> 40
cf3ca219c727 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 365
diff changeset
    38
365
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
val C = Set("a", "b", "")
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
pow(C, 2)
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
pow(C, 2).size                            // -> 7
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
pow(C, 3)
9b71dead1219 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
pow(C, 3).size                            // -> 15
525
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    45
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    46
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    47
//val A = Set("a", "b", "c", "d")
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    48
//pow(A, 4).size   
525
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    49
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    50
//val A = Set("a", "b", "c")
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    51
//pow(A, 5).size   
525
a2ee4b11c976 updated
cu
parents: 471
diff changeset
    52
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    53
//val A = Set("a", "b", "")
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    54
//pow(A, 5).size   
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
    55
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
    56
572
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    57
for (n <- (0 to 6).toList) 
4a1739f256fd updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
    58
  yield pow(B, n).size
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    59
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    60
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    61
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    62
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    63
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    64
val A = Set("a", "b", "c")
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    65
pow(A, 3)
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    66
pow(A, 3).size
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    67
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    68
val B = Set("a", "b", "")
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 572
diff changeset
    69
pow(B, 4)
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    70
pow(B, 4).size
765
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    71
768
34f77b976b88 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 765
diff changeset
    72
val C = Set("a", "")
34f77b976b88 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 765
diff changeset
    73
pow(C, 4)
34f77b976b88 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 765
diff changeset
    74
pow(C, 4).size
765
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    75
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    76
val SA = Set("a", "b", "c", "d")
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    77
val SB = Set("0", "1", "2", "3", "4", "5", "6")
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    78
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    79
concat(SA, SB).size
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    80
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    81
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    82
val SA2 = Set("ab", "a", "c", "d")
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    83
val SB2 = Set("bc", "c", "2", "3", "4", "5", "6")
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    84
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    85
concat(SA2, SB2).size
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    86
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    87
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    88
/*
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    89
((a b) c) d
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    90
a (b (c d))
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    91
(a b) (c d)
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    92
(a (b c)) d
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    93
a ((b c) d)
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 676
diff changeset
    94
*/