progs/lecture2.scala
changeset 174 90e0b1cc460b
parent 173 2de1f79dedf0
child 192 a112e0e2325c
equal deleted inserted replaced
173:2de1f79dedf0 174:90e0b1cc460b
   450 // A return expression, when evaluated, abandons the 
   450 // A return expression, when evaluated, abandons the 
   451 // current computation and returns to the caller of the 
   451 // current computation and returns to the caller of the 
   452 // function in which return appears."
   452 // function in which return appears."
   453 
   453 
   454 def sq1(x: Int): Int = x * x
   454 def sq1(x: Int): Int = x * x
       
   455 def sumq(ls: List[Int]): Int = 
       
   456   ls.map(x => x * x).sum
       
   457 
       
   458 
       
   459 
       
   460 
   455 def sq2(x: Int): Int = return x * x
   461 def sq2(x: Int): Int = return x * x
   456 
   462 
   457 def sumq(ls: List[Int]): Int = {
   463 def sumq(ls: List[Int]): Int = {
   458   ls.map(sq1).sum[Int]
   464   ls.map(sq1).sum[Int]
   459 }
   465 }
   465 def sumq(ls: List[Int]): Int = {
   471 def sumq(ls: List[Int]): Int = {
   466   val sqs : List[Int] = for (x <- ls) yield (return x * x)
   472   val sqs : List[Int] = for (x <- ls) yield (return x * x)
   467   sqs.sum
   473   sqs.sum
   468 }
   474 }
   469 
   475 
       
   476 sumq(List(1, 2, 3, 4))
   470 
   477 
   471 
   478 
   472 
   479 
   473 // Type abbreviations
   480 // Type abbreviations
   474 //====================
   481 //====================