Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //zadanie 1
- //def exists[A](xs:List[A])(p:A=>Boolean):Boolean=
- //xs match {
- // case h::t => if(p(h)) true else exists(t)(p)
- // case Nil => false
- //}
- //
- //exists (List(5,1,2,3)) (_ == 2) == true
- //exists (List(5,1,2,3)) (_ == 6) == false
- //exists (List()) (_ == 6) == false
- //
- //def existsFL[A](xs:List[A])(p:A=>Boolean):Boolean=
- // (xs foldLeft false)((x:Boolean,y:A)=> x || p(y))
- //
- //existsFL (List(5,1,2,3)) (_ == 2) == true
- //existsFL (List(5,1,2,3)) (_ == 6) == false
- //existsFL (List()) (_ == 6) == false
- //
- //def existsFR[A](xs:List[A])(p:A=>Boolean):Boolean=
- // (xs foldRight false)((x:A, y:Boolean)=> y || p(x))
- //
- //existsFR (List(5,1,2,3)) (_ == 2) == true
- //existsFR (List(5,1,2,3)) (_ == 6) == false
- //existsFR (List()) (_ == 6) == false
- //
- //zadanie2
- //def filter[A](xs:List[A])(p:A=>Boolean):List[A]=
- // (xs foldRight List[A]()) ((x:A, y:List[A]) => if(p(x)) x::y else y)
- //
- //filter (List(2,7,1,3,7,8,4,1,6,9)) (_ > 3) == List(7, 7, 8, 4, 6, 9)
- //filter (List(0)) (_ > 3) == List()
- //
- //zadanie3
- //def remove1[A](xs:List[A])(p:A=>Boolean):List[A]=
- // xs match {
- // case h::t => if(p(h)) t else h::remove1(t)(p)
- // case _ => Nil
- //}
- //
- //remove1(List(1,2,3,2,5)) (_ == 2) == List(1, 3, 2, 5)
- //remove1(List(1,2,3,2,5)) (_ == 6) == List(1,2,3,2,5)
- //remove1(List()) (_ == 6) == List()
- //
- //
- //zadanie4
- //def splitAt[A](xs:List[A])(n:Int):(List[A],List[A])= {
- // def splitAtAcc(xs: List[A])(acc1: List[A], acc2: List[A])(n: Int)
- // : (List[A], List[A]) =
- // xs match {
- // case h :: t if n > 0 => splitAtAcc(t)(h::acc1, acc2)(n-1)
- // case _ => (acc1.reverse, xs)
- // }
- // splitAtAcc(xs)(List(), List())(n)
- //}
- //
- //splitAt (List('a','b','c','d','e')) (2)
- //splitAt (List('a','b','c','d','e')) (2) == (List('a', 'b'), List('c', 'd', 'e'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement