progs/lecture3.scala
changeset 153 4383809c176a
parent 152 114a89518aea
child 155 371acb50643d
equal deleted inserted replaced
152:114a89518aea 153:4383809c176a
    49 bcollatz2(100000000000000000L.toBinaryString)
    49 bcollatz2(100000000000000000L.toBinaryString)
    50 bcollatz2(BigInt("1000000000000000000000000000000000000000000000000000000000000000000000000000").toString(2))
    50 bcollatz2(BigInt("1000000000000000000000000000000000000000000000000000000000000000000000000000").toString(2))
    51 
    51 
    52 
    52 
    53 
    53 
    54 // One of only two places where I conceded to mutable
    54 // Roman Numerals
    55 // data structures: The following function generates 
    55 abstract class RomanDigit 
    56 // new labels
    56 case object I extends RomanDigit 
    57 
    57 case object V extends RomanDigit 
    58 var counter = -1
    58 case object X extends RomanDigit 
    59 
    59 case object L extends RomanDigit 
    60 def fresh(x: String) = {
    60 case object C extends RomanDigit 
    61   counter += 1
    61 case object D extends RomanDigit 
    62   x ++ "_" ++ counter.toString()
    62 case object M extends RomanDigit 
    63 }
    63 
    64 
    64 type RomanNumeral = List[RomanDigit] 
    65 fresh("x")
    65 
    66 fresh("x")
    66 def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
    67 
    67   case Nil => 0
    68 // this can be avoided, but would have made my code more
    68   case M::r    => 1000 + RomanNumeral2Int(r)  
    69 // complicated
    69   case C::M::r => 900 + RomanNumeral2Int(r)
       
    70   case D::r    => 500 + RomanNumeral2Int(r)
       
    71   case C::D::r => 400 + RomanNumeral2Int(r)
       
    72   case C::r    => 100 + RomanNumeral2Int(r)
       
    73   case X::C::r => 90 + RomanNumeral2Int(r)
       
    74   case L::r    => 50 + RomanNumeral2Int(r)
       
    75   case X::L::r => 40 + RomanNumeral2Int(r)
       
    76   case X::r    => 10 + RomanNumeral2Int(r)
       
    77   case I::X::r => 9 + RomanNumeral2Int(r)
       
    78   case V::r    => 5 + RomanNumeral2Int(r)
       
    79   case I::V::r => 4 + RomanNumeral2Int(r)
       
    80   case I::r    => 1 + RomanNumeral2Int(r)
       
    81 }
       
    82 
       
    83 RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid roman number)
       
    84 RomanNumeral2Int(List(I,V))             // 4
       
    85 RomanNumeral2Int(List(V,I))             // 6
       
    86 RomanNumeral2Int(List(I,X))             // 9
       
    87 RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
       
    88 RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
    70 
    89 
    71 
    90 
    72 // Tail recursion
    91 // Tail recursion
    73 //================
    92 //================
    74 
    93 
   299   def % = STAR(s)
   318   def % = STAR(s)
   300   def ~ (r: Rexp) = SEQ(s, r)
   319   def ~ (r: Rexp) = SEQ(s, r)
   301   def ~ (r: String) = SEQ(s, r)
   320   def ~ (r: String) = SEQ(s, r)
   302 }
   321 }
   303 
   322 
       
   323 //example regular expressions
   304 val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
   324 val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
   305 val sign = "+" | "-" | ""
   325 val sign = "+" | "-" | ""
   306 val number = sign ~ digit ~ digit.% 
   326 val number = sign ~ digit ~ digit.% 
       
   327 
       
   328 
       
   329 //implement print_re
   307 
   330 
   308 
   331 
   309 
   332 
   310 // Lazyness with style
   333 // Lazyness with style
   311 //=====================
   334 //=====================