SHARE
TWEET

Untitled

a guest Mar 18th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. object Pierwsze extends App{
  2.  
  3. //przykladowe definicje funkcji skladowych
  4. def f(a:Int):Int={
  5.     a*2
  6. }
  7. def g(b:Int):Int={
  8.     b*b
  9. }
  10. def op(a:Int,b:Int):Int={
  11.    a+b
  12. }
  13.  
  14. def compose[A,B,C](f: A => B)(g: B => C) : A => C ={
  15.     x:A => g(f(x))
  16. }
  17.  
  18. def prod[A,B,C,D](f: A => C, g: B => D): (A, B) => (C, D) ={
  19.    (x:A, y:B) => (f(x), g(y))
  20. }
  21.  
  22. def lift[A,T](op: (T,T) => T)(f: A => T, g: A => T): A => T = {
  23.       (a: A) => op(f(a), g(a))
  24. }
  25.  
  26. //przykladowe wywolania
  27.  
  28. print(compose(f)(g)(5)+"\n")
  29. //100 bo g(f(5))=g(5*2)=g(10)=10*10
  30. print(prod(f,g)(4,5)+"\n")
  31. //(8,25) bo (f(4),g(5))=(4*2,5*5)
  32. print(lift(op)(f,g)(5))
  33. //35 bo op((f(5),g(5)))=op(5*2,5*5)=op(10,25)=10+25
  34. }
  35.  
  36.  
  37.  
  38. object Czwarte extends App{
  39.  
  40.     //ogonowa
  41.     def size[A](a:Seq[A]):Int={
  42.          def sizeAcc[A](a: Seq[A], length :Int): Int={
  43.             if(a.isEmpty) length
  44.             else sizeAcc(a.tail,length+1)
  45.         }
  46.         sizeAcc(a,0)
  47.     }
  48.  
  49.     //zwykla
  50.     def sizeZwykla[A](a:Seq[A]):Int={
  51.         if(a.isEmpty) 1
  52.         else 1+size(a.tail)
  53.     }
  54.  
  55.    
  56.     print(size(Seq(1,2,3)))
  57.     print(sizeZwykla(Seq(1,2,3,4)))
  58.    
  59.  
  60.  
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top