Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. sealed trait List[+A]
  2.  
  3. case object Nil extends List[Nothing]
  4. case class Cons[+A](head: A, tail: List[A]) extends List[A]
  5.  
  6. object List {
  7. def sum(ints: List[Int]): Int = ints match {
  8. foldLeft(l, 0.0)(_+_)
  9. }
  10.  
  11. def product(ds: List[Double]): Double = ds match {
  12. foldLeft(l, 1.0)(_*_)
  13. }
  14.  
  15. def apply[A](as: A*): List[A] =
  16. if (as.isEmpty) Nil
  17. else Cons(as.head, apply(as.tail:_*))
  18.  
  19. def tail[A](as: List[A]): List[A] = as match {
  20. case Nil => Nil
  21. case Cons(_,t) => t
  22. }
  23.  
  24. def drop[A](l: List[A], n: Int) l match {
  25. case Nil => println("Empty list given")
  26. case Cons(_,t)=> drop(t,n-1)
  27. }
  28.  
  29. def dropWhile[A](l:List[A])(f: A=>Boolean):List[A] = l match {
  30. case _ => l
  31. case Cons(h,t)=> if (f(h)) dropWhile(t,f)
  32. }
  33.  
  34. def setHead[A](l: List[A], h: Int): List[A] = l match {
  35. case Cons(_,t) => Cons(h, t)
  36. case _ => h
  37. }
  38.  
  39. def init[A](l: List[A]): List[A] = l match {
  40. case Nil => Nil
  41. case Cons(h,t) => t
  42. }
  43.  
  44. def foldRight[A,B](l: List[A], z: B)(f: (A,B)=>B): B =
  45. l match {
  46. case Nil => z
  47. case Cons(x,xs) => f(x, foldRight(xs,z)(f))
  48. }
  49.  
  50. @annotation.tailrec
  51. def foldLeft[A,B](l: List[A], z: B)(f: (B, A) => B): B = l match {
  52. case Nil => z
  53. case Cons(h,t) => foldLeft(t, f(z,h))(f)
  54. }
  55.  
  56. def reverse[A](l: List[A]): List[A] =
  57. foldLeft(l, List[A]())((acc,h) => Cons(h,acc))
  58.  
  59.  
  60. def sum2(l: List[Int]) =
  61. foldRight(l, 0.0)(_+_)
  62.  
  63. def product2(l: List[Double]) =
  64. foldRight(l, 1.0)(_*_)
  65.  
  66. def append[A](l1: List[A], l2: List[A]): List[A] =
  67. foldLeft(l1, l2)(Cons(_,_))
  68.  
  69. def concat[A](l: List[List[A]]): List[A] =
  70. foldRight(l, Nil:List[A])(append)
  71.  
  72. def add1(l: List[Int]): List[Int] =
  73. foldRight(l, Nil:List[Int])(h,t)=>(Cons(h+1, t))
  74.  
  75. def doubleToString(l: List[Double]): List[String] =
  76. foldRight(l, Nil:List[String])(h,t)=>(Cons(h toString, t))
  77.  
  78. def map[A,B](l: List[A])(f: A=>B): List[B] =
  79. foldRight(l, Nil:List[B])((h,t)=Cons(f(h),t))
  80.  
  81. def flatMap[A,B](l: List[A])(f: A=>List[B]): List[B] =
  82. concat(map(l)(f))
  83.  
  84. def filter[A](l: List[A])(f: A=>A): List[A] =
  85. foldRight(l, Nil:List[A])((h,t) => if (f(h)) Cons(h,t) else t)
  86.  
  87. def filterViaFlatMap[A](l: List[A])(f: A=>A): List[A] =
  88. flatMap(l)(a)=>if (f(a)) List(a) else Nil
  89.  
  90. def fPairwise[A,B,C](a: List[A], b: List[B])(f: (A,B)=>C): List[C] = (a,b) match {
  91. case (Nil, _) => Nil
  92. case (_, Nil) => Nil
  93. case (Cons(h1,t1), Cons(h2,t2)) => Cons(f(h1,h2), addPairwise(t1,t2)(f))
  94. }
  95.  
  96. def take[A]( n: Int): List[A] =
  97. filter(this)(a)=>(a<n)
  98.  
  99. def takeWhile[A](l: List)(f: A=>Boolean): List[A] = l match {
  100. case _ => Nil
  101. case Cons(h,t) => if f(h) => h :: takeWhile(t)(f)
  102. }
  103.  
  104.  
  105. def forall(f: A=>Boolean): Boolean =
  106. takeWhile(this, f) match {
  107. case _ => false
  108. case this => true
  109. }
  110.  
  111. def exists(f: A=>Boolean): Boolean =
  112. takeWhile(this, f) match {
  113. case _ => false
  114. case Cons(h,t) => true
  115. }
  116.  
  117.  
  118.  
  119. def length[A](l: List[A]): Int =
  120. foldLeft(l, 0)((_, acc)=>acc+1)
  121.  
  122. @annotation.tailrec
  123. def startsWith[A](l: List[A], prefix: List[A]): Boolean = (l,prefix) match {
  124. case (_,Nil) => true
  125. case (Cons(h,t),Cons(h2,t2)) if h == h2 => startsWith(t, t2)
  126. case _ => false
  127. }
  128.  
  129. @annotation.tailrec
  130. def hasSubsequence[A](sup: List[A], sub: List[A]): Boolean = sup match {
  131. case Nil => sub == Nil
  132. case _ if startsWith(sup, sub) => true
  133. case Cons(_,t) => hasSubsequence(t, sub)
  134. }
  135.  
  136.  
  137. val example = Cons(1, Cons(2, Cons(3, Nil)))
  138. val example2 = List(1,2,3)
  139. val total = sum(example)
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement