progs/lecture5.scala
changeset 470 86a456f8cb92
parent 455 557d18cce0f0
child 481 e03a0100ec46
equal deleted inserted replaced
469:48de09728447 470:86a456f8cb92
   269 
   269 
   270 square(42 + 8)
   270 square(42 + 8)
   271 
   271 
   272 // This is called "strict evaluation".
   272 // This is called "strict evaluation".
   273 
   273 
   274 // On the contrary, say we have a pretty expensive operation:
   274 // In contrast say we have a pretty expensive operation:
   275 
   275 
   276 def peop(n: BigInt): Boolean = peop(n + 1) 
   276 def peop(n: BigInt): Boolean = peop(n + 1) 
   277 
   277 
   278 val a = "foo"
   278 val a = "foo"
   279 val b = "foo"
   279 val b = "foo"
   392 }
   392 }
   393 
   393 
   394 enuml(1, "a")
   394 enuml(1, "a")
   395 enuml(1, "a").size
   395 enuml(1, "a").size
   396 enuml(2, "a").size
   396 enuml(2, "a").size
   397 enuml(3, "a").size // out of heap space
   397 enuml(3, "a").size 
   398 
   398 enuml(4, "a").size // out of heap space
   399 
   399 
   400 
   400 
   401 def enum(rs: LazyList[Rexp]) : LazyList[Rexp] = 
   401 def enum(rs: LazyList[Rexp]) : LazyList[Rexp] = 
   402   rs #::: enum( (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
   402   rs #::: enum( (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
   403                 (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
   403                 (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
   404                 (for (r1 <- rs) yield STAR(r1)) )
   404                 (for (r1 <- rs) yield STAR(r1)) )
   405 
   405 
   406 
   406 
   407 enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(200).force
   407 enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(200).force
   408 enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(5_000_000).force
   408 enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(5_000_000).force // out of memory
   409 
   409 
   410 
   410 
   411 def depth(r: Rexp) : Int = r match {
   411 def depth(r: Rexp) : Int = r match {
   412   case ZERO => 0
   412   case ZERO => 0
   413   case ONE => 0
   413   case ONE => 0