Advertisement
Guest User

syöks scala

a guest
Apr 1st, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.33 KB | None | 0 0
  1. object Sisakkain extends App {
  2.   val a=1; val b=2; val c=3
  3.  
  4.   def f = {
  5.     val b=20; val c=30
  6.    
  7.     def ff = {
  8.       val c=300
  9.       println(a+"/"+b+"/"+c) // 1/20/300
  10.     }
  11.    
  12.     println(a+"/"+b+"/"+c) // 1/20/30
  13.     ff
  14.   }
  15.  
  16.   def g = {
  17.    
  18.     def gg = {
  19.       f
  20.       60
  21.     }
  22.    
  23.     val a=40; val b=gg;
  24.     println(a+"/"+b+"/"+c) // 40/60/3
  25.     f
  26.   }
  27.  
  28.   println(a+"/"+b+"/"+c) // 1/2/3
  29.   f;
  30.  
  31.   { val a=1000
  32.     g
  33.     println(a+"/"+b+"/"+c) // 1000/2/3
  34.   }
  35.  
  36. }
  37.  
  38. object Kirjasto {
  39.   def summa(termi: (Int) => Double, lkm: Int) = {
  40.     var s=0.0
  41.     for (i <- 1 to lkm) s += termi(i)
  42.     s
  43.   }
  44. }
  45.  
  46. object Sovellus extends App {
  47.  
  48.   def harm(i: Int) = 1.0/i
  49.   println(  Kirjasto.summa(harm, 4) ) // 1/1 + 1/2 + 1/3 + 1/4
  50.  
  51.  
  52.   def geom(i: Int) = 1.0/(i*i)  
  53.   println(  Kirjasto.summa(geom, 4) ) // 1/(1*1) + 1/(2*2) + 1/(3*3) + 1/(4*4)
  54.  
  55.  
  56.   println(  Kirjasto.summa(_ +0.5, 4) ) // 1.5 + 2.5 + 3.5 + 4.5
  57.  
  58.  
  59.   var laskuri=0.0  
  60.   def mitaMita(i: Int) = {laskuri+=1; laskuri}
  61.   println(  Kirjasto.summa(mitaMita, 4) ) // 1 + 2 + 3 + 4
  62.  
  63.  
  64.   var ed=1; var seur=1; var alussa=1
  65.   def f(i: Int) = if (alussa < 3)
  66.                      {alussa+=1; 1}
  67.                   else
  68.                      {val uus=ed+seur; ed=seur; seur=uus; uus}
  69.   println(  Kirjasto.summa(f, 4) ) //1 + 1 + 2 + 3 +
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement