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