• API
• FAQ
• Tools
• Archive
daily pastebin goal
0%
SHARE
TWEET

# Untitled

a guest Mar 19th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.
3. //def exists[A](xs:List[A])(p:A=>Boolean):Boolean=
4. //xs match {
5. //  case h::t => if(p(h)) true else exists(t)(p)
6. //  case Nil => false
7. //}
8. //
9. //exists (List(5,1,2,3)) (_ == 2)  ==   true
10. //exists (List(5,1,2,3)) (_ == 6)  ==   false
11. //exists (List()) (_ == 6)  ==   false
12. //
13. //def existsFL[A](xs:List[A])(p:A=>Boolean):Boolean=
14. //  (xs foldLeft false)((x:Boolean,y:A)=> x || p(y))
15. //
16. //existsFL (List(5,1,2,3)) (_ == 2)  ==   true
17. //existsFL (List(5,1,2,3)) (_ == 6)  ==   false
18. //existsFL (List()) (_ == 6)  ==   false
19. //
20. //def existsFR[A](xs:List[A])(p:A=>Boolean):Boolean=
21. //  (xs foldRight false)((x:A, y:Boolean)=> y || p(x))
22. //
23. //existsFR (List(5,1,2,3)) (_ == 2)  ==   true
24. //existsFR (List(5,1,2,3)) (_ == 6)  ==   false
25. //existsFR (List()) (_ == 6)  ==   false
26. //
28. //def filter[A](xs:List[A])(p:A=>Boolean):List[A]=
29. //  (xs foldRight List[A]()) ((x:A, y:List[A]) => if(p(x)) x::y else y)
30. //
31. //filter (List(2,7,1,3,7,8,4,1,6,9)) (_ > 3) == List(7, 7, 8, 4, 6, 9)
32. //filter (List(0)) (_ > 3) == List()
33. //
35. //def remove1[A](xs:List[A])(p:A=>Boolean):List[A]=
36. //  xs match {
37. //    case h::t => if(p(h)) t else h::remove1(t)(p)
38. //    case _ => Nil
39. //}
40. //
41. //remove1(List(1,2,3,2,5)) (_ == 2) == List(1, 3, 2, 5)
42. //remove1(List(1,2,3,2,5)) (_ == 6) == List(1,2,3,2,5)
43. //remove1(List()) (_ == 6) == List()
44. //
45. //
47. //def splitAt[A](xs:List[A])(n:Int):(List[A],List[A])= {
48. //  def splitAtAcc(xs: List[A])(acc1: List[A], acc2: List[A])(n: Int)
49. //  : (List[A], List[A]) =
50. //    xs match {
51. //      case h :: t if n > 0 => splitAtAcc(t)(h::acc1, acc2)(n-1)
52. //      case _ => (acc1.reverse, xs)
53. //    }
54. //  splitAtAcc(xs)(List(), List())(n)
55. //}
56. //
57. //splitAt (List('a','b','c','d','e')) (2)
58. //splitAt (List('a','b','c','d','e')) (2) == (List('a', 'b'), List('c', 'd', 'e'))
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.

Top