progs/lecture1.scala
changeset 359 8aaf187d25f0
parent 356 d1046d9d3213
child 360 e45d2890749d
equal deleted inserted replaced
358:542d2cf143b0 359:8aaf187d25f0
   242 fact(150)
   242 fact(150)
   243 
   243 
   244 /* boolean operators
   244 /* boolean operators
   245  
   245  
   246    ==     equals
   246    ==     equals
       
   247    !=     not equals
   247    !      not
   248    !      not
   248    && ||  and, or
   249    && ||  and, or
   249 */
   250 */
   250 
   251 
   251 
   252 
   252 def fact2(n: BigInt) : BigInt = 
   253 
   253   if (n == 0) 1 else n * fact2(n - 1)
   254 def fib(n: Int) : Int = {
   254 
       
   255 fact2(150)
       
   256 
       
   257 def fib(n: Int) : Int =
       
   258   if (n == 0) 1 else
   255   if (n == 0) 1 else
   259     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
   256     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
       
   257 }
       
   258 
       
   259 fib(9)
       
   260 
       
   261 
   260 
   262 
   261 
   263 
   262 //gcd - Euclid's algorithm
   264 //gcd - Euclid's algorithm
   263 
   265 
   264 def gcd(a: Int, b: Int) : Int = {
   266 def gcd(a: Int, b: Int) : Int = {
   275 power(5, 5)
   277 power(5, 5)
   276 
   278 
   277 // BTW: no returns!!
   279 // BTW: no returns!!
   278 // "last" line (expression) in a function determines the 
   280 // "last" line (expression) in a function determines the 
   279 // result
   281 // result
       
   282 
       
   283 def average(xs: List[Int]) : Int = {
       
   284   if (xs.length == 0) 0 
       
   285   else xs.sum / xs.length
       
   286 }
       
   287 
       
   288 average(List())
       
   289 
   280 
   290 
   281 
   291 
   282 // For-Comprehensions (not For-Loops)
   292 // For-Comprehensions (not For-Loops)
   283 //====================================
   293 //====================================
   284 
   294